Open Shrulabh opened 3 years ago
`import 'dart:io';
import 'package:flutter/material.dart'; import 'package:media_gallery/media_gallery.dart'; import 'package:permission_handler/permission_handler.dart';
class ListAppScreen extends StatefulWidget { @override _ListAppScreenState createState() => _ListAppScreenState(); }
class _ListAppScreenState extends State { List imageList = []; _listMedia() { Permission.storage.request().isGranted.then( (value) { if (value) { print(value); if (imageList.length == 0) { fetchMediaFromPhone(); } } else { print(imageList.length); Permission.storage.request(); } }, ); print(imageList.length); return imageList.length == 0 ? Container(child: Center(child: CircularProgressIndicator())) : Container( child: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3), itemCount: imageList.length, itemBuilder: (BuildContext context, index) { return Container( decoration: BoxDecoration( border: Border.all( width: 10, color: Colors.black38, ), ), child: Image.file(imageList[index]), ); }, ), ); }
void fetchMediaFromPhone() { MediaGallery.listMediaCollections( mediaTypes: [MediaType.image], ).then((value) { print(value); for (MediaCollection collections in value) { collections .getMedias( mediaType: MediaType.image, take: 500, ) .then( (mediaPage) async { for (var i in mediaPage.items) { File mediaFile = await i.getFile(); if (!imageList.contains(mediaFile)) { imageList.add(mediaFile); } print("item info of media type - ${mediaFile.path}"); } setState(() {}); // break; }, ); break; } }); }
void selectImage(BuildContext ctx) { Navigator.of(ctx).pushNamed( '/app', ); }
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: _listMedia(), ), ); } }`
This the above code. I am trying to display all the images in the gallery, when the page loads up it is able to show only 4-5 images and then the app crashes. Please help..
Hi !
Do you have any log ? Maybe a memory issue.
Did you try the provided sample ? Does it work ?
`import 'dart:io';
import 'package:flutter/material.dart'; import 'package:media_gallery/media_gallery.dart'; import 'package:permission_handler/permission_handler.dart';
class ListAppScreen extends StatefulWidget { @override _ListAppScreenState createState() => _ListAppScreenState(); }
class _ListAppScreenState extends State {
List imageList = [];
_listMedia() {
Permission.storage.request().isGranted.then(
(value) {
if (value) {
print(value);
if (imageList.length == 0) {
fetchMediaFromPhone();
}
} else {
print(imageList.length);
Permission.storage.request();
}
},
);
print(imageList.length);
return imageList.length == 0
? Container(child: Center(child: CircularProgressIndicator()))
: Container(
child: GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemCount: imageList.length,
itemBuilder: (BuildContext context, index) {
return Container(
decoration: BoxDecoration(
border: Border.all(
width: 10,
color: Colors.black38,
),
),
child: Image.file(imageList[index]),
);
},
),
);
}
void fetchMediaFromPhone() { MediaGallery.listMediaCollections( mediaTypes: [MediaType.image], ).then((value) { print(value); for (MediaCollection collections in value) { collections .getMedias( mediaType: MediaType.image, take: 500, ) .then( (mediaPage) async { for (var i in mediaPage.items) { File mediaFile = await i.getFile(); if (!imageList.contains(mediaFile)) { imageList.add(mediaFile); } print("item info of media type - ${mediaFile.path}"); } setState(() {}); // break; }, ); break; } }); }
void selectImage(BuildContext ctx) { Navigator.of(ctx).pushNamed( '/app', ); }
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: _listMedia(), ), ); } }`
This the above code. I am trying to display all the images in the gallery, when the page loads up it is able to show only 4-5 images and then the app crashes. Please help..