This is my code.
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_storage/firebase_storage.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Wallpaper App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WallpaperScreen(),
);
}
}
class WallpaperScreen extends StatefulWidget {
@override
_WallpaperScreenState createState() => _WallpaperScreenState();
}
class _WallpaperScreenState extends State {
List imageUrls = []; // Store fetched image URLs
@override
void initState() {
super.initState();
fetchImages();
}
Future fetchImages() async {
try {
final storageRef = FirebaseStorage.instance.ref();
final result = await storageRef.listAll();
for (var item in result.items) {
try {
final url = await item.getDownloadURL();
setState(() {
imageUrls.add(url);
});
} catch (error) {
print("Error fetching URL");
}
}
} catch (e) {
print("Error fetching images: $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Wallpapers'),
),
body: GridView.builder(
itemCount: imageUrls.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
),
itemBuilder: (BuildContext context, int index) {
return Image.network(
imageUrls[index], // Load image from URL
fit: BoxFit.cover,
);
},
),
);
}
}
0 comments:
Post a Comment
Thanks