我决定把我的一些图像档案从我的“彩虹”应用的火基储存中取出,因为图像的总尺寸约为25gb。 当我想要拉动图像时,我无法被称作警告,我如何解决这一问题?
If you can t get an idea about the code, here s a hint: I m making an application that displays images with a page-turning animation. so the firebase operation must be compatible with this.
警告
I/flutter ( 6311): Error getting image URL: [core/no-app] No Firebase App [DEFAULT] has been created - call Firebase.initializeApp()
I/chatty ( 6311): uid=10148(com.example.libsy_app) 1.ui identical 2 lines
I/flutter ( 6311): Error getting image URL: [core/no-app] No Firebase App [DEFAULT] has been created - call Firebase.initializeApp()
我的这部法律
import package:firebase_core/firebase_core.dart ;
import package:flutter/material.dart ;
import package:page_flip/page_flip.dart ;
import package:shared_preferences/shared_preferences.dart ;
import package:firebase_storage/firebase_storage.dart as firebase_storage;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(AChristmasCarol());
}
final _controller = GlobalKey<PageFlipWidgetState>();
class AChristmasCarol extends StatefulWidget {
const AChristmasCarol({Key? key}) : super(key: key);
@override
State<AChristmasCarol> createState() => _AChristmasCarolState();
}
class _AChristmasCarolState extends State<AChristmasCarol> {
final _controller = GlobalKey<PageFlipWidgetState>();
// sharedPreferences to store lastLeftOverPage Index
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
// last left over page index
int lastLeftOverPageIndex = 0;
// SharedPreference key to retrieve the value of lastLeftOverPageIndex
String lastLeftOverPageNoPrefKey = "_lastLeftOverPageNoPrefKey";
final List<String> imagesPath = [
/books/A Christmas Carol/page 1.png ,
/books/A Christmas Carol/page 2.png ,
/books/A Christmas Carol/page 3.png ,
/books/A Christmas Carol/page 4.png ,
];
late List<String> downloadUrls;
Widget _buildDemoPage(String imageUrl) {
return FutureBuilder(
future: _getImageUrl(imageUrl),
builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasError || snapshot.data == null) {
return Center(child: Text( Error fetching image ));
}
return Scaffold(
body: Image.network(
snapshot.data!,
fit: BoxFit.cover,
),
);
},
);
}
Future<String?> _getImageUrl(String imageUrl) async {
try {
final url = await firebase_storage.FirebaseStorage.instance
.ref()
.child(imageUrl)
.getDownloadURL();
return url;
} catch (e) {
print( Error getting image URL: $e );
return null;
}
}
@override
void initState() {
// restore the previous lastLeftOverPageIndex;
_restoreLeftOverPage();
super.initState();
}
Future<void> _restoreLeftOverPage() async {
SharedPreferences pref = await _prefs;
lastLeftOverPageIndex =
pref.getInt(lastLeftOverPageNoPrefKey)?.toInt() ?? 0;
// navigate the book page index to lastLeftOverPageIndex
_controller.currentState?.goToPage(lastLeftOverPageIndex);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageFlipWidget(
key: _controller,
backgroundColor: Colors.white,
initialIndex: lastLeftOverPageIndex,
lastPage: Container(
color: Colors.white, child: const Center(child: Text( The End! ))),
children: [
for (var imagePath in imagesPath) _buildDemoPage(imagePath),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.looks_one_outlined),
onPressed: () {
_controller.currentState?.goToPage(1);
},
),
);
}
@override
void dispose() {
super.dispose();
}
@override
void deactivate() {
super.deactivate();
// before disposing the widget save the current page no
var currentPageNo = _controller.currentState?.pageNumber.toInt() ?? 0;
saveLastLeftOverPagePref(currentPageNo);
}
Future<void> saveLastLeftOverPagePref(int lastPage) async {
SharedPreferences pref = await _prefs;
pref.setInt(lastLeftOverPageNoPrefKey, lastPage);
}
}