how to view List<Image>? - flutter

I'm converting the files I receive from my API as Images and add it to my List<Image> imageList;
How can I then show it using Listview.builder?
I tried using
ListView.builder(
itemCount: imageList.length,
itemBuilder: (BuildContext context, int index) {
return Image.memory(imageList[index].getBytes());
});
but it returns an error
Exception: Could not instantiate image codec.
Are there other ways? Thanks for those who can help.

Related

Fetching data properly with Futurebuilder and json response

How can I render my response in my text widget?
My json snapshot.data is as following:
"{\"0\":{\"Breed\":\"American Hairless Terrier\",\"count\":1},\"1\":{\"Breed\":\"Bolognese\",\"count\":2},\"2\":{\"Breed\":\"Cavalier King Charles Spaniel\",\"count\":12},\"3\":{\"Breed\":\"Central Asian Shepherd Dog\",\"count\":1},\"4\":{\"Breed\":\"Papillon\",\"count\":1}}"
I tried to display my data like this:
Text(snapshot.data[index.toString()]['Breed']),
but I am getting:
type 'String' is not a subtype of type 'int' of 'index'
try this, might not be perfect but i will give you some idea, the error is because you are assigning int value to Text widget
Text((snapshot.data[index].
['Breed']).toString());
if you want to show it in
futureBuilder and listview
here:
FutureBuilder(
future: FirebaseFirestore.
instance.
collection("groups").
doc(groupId).snapshots(),
//here your collection name
// and doc id
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
}
var userDocument = snapshot.data["Breeds"];
return ListView.builder(
itemCount: userDocument.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Text(userDocument[index]);
),
}
);
The indices 0,1,2,3.. are strings(wrapped with quotation marks). But you are providing int.
Try
Text(snapshot.data['Breed'][index.toString()])

Filtering a List in Flutter using Riverpod and FutureProvider

i am learning Flutter and currently using Riverpod + Hooks as a State Management. Following several tutorials and articles i have succesfully retrieved data from an API and is showing correctly using a FutureProvider with .when clause
final caseEntries = ref.watch(caseEntriesFutureProvider);
//And the UI looks like this
caseEntries.when(
data: (items) {
return GridView.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 380.0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
final item = items[index];
return CaseItem(item: item);
},
);
},
error: ((obj, error) => Text(error.toString())),
loading: (() => const material.CircularProgressIndicator()),
),
Now, how can i filter the collection using a search term? Every tutorial i came across is either using a prepopulated List and filtering using an enum or doing some extra complicated stuff and not using a FutureProvider
EDIT: Added more sample code
#override
Widget build(BuildContext context, WidgetRef ref) {
final caseEntries = ref.watch(caseEntriesFutureProvider);
final searchTextController = useTextEditingController(text:"");
useEffect(() {
searchTextController.addListener(() {});
}, [searchTextController]);

Flutter/Dart Non-Nullable - Use Snapshot data directly

I am using expansion tiles in my code, and was using snapshot.data.length then accessing data directly from the snapshot when I was using the nullable version of Dart.
However, I changed it down the line to the non-nullable version, and it is throwing the errors below -- how can I access the snapshot data, or convert it into a List/Map to be able to use it?
The attached image shows the errors it's showing me. I tried converting the snapshot.data to another var but that didn't work either.
So I hope this solves your problem. Attached the code and some comments. I tested it in Dartpad and it worked fine like that. If it's still not working please provide more code.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
// you need to define what the future builder will return
return FutureBuilder<List<BudgetItem>>(
// helper.getAllItems() needs to have a return type
// "Future<List<BudgetItem>>"
future: helper.getAllItems(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
// snapshot.data[index] is now from type "BudgetItem" and you can
// access whatever properties it has
return ListTile(title: Text(snapshot.data![index].title));
},
);
}
// if there is no data you still need to return sth like a loading spinner
return Container();
},
);
}

How to mix stream with Provider?

I am use Provider. I want mix different data source with stream.
Use case: Chat app where some message are from system (date/error message) but other are from database (Firestore).
For example for just get message from database I now use StreamBuilder:
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('message').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return new ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
return messageBuilder(snapshot.data.documents[index], xa);
});
But with StreamBuilder cannot mix data from other source.
I want inject message at messages[index] for different message type.
Possible solution is create separate messages List and feed into ListView.builder:
return new ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
final message = messages[index];
return messageBuilder(message, xa);
});
But how I can use Provider to mix stream from Firestore and also system message into messages List?
How I can bring together data source into final List messages?
Thanks for help!

AsyncSnapshot rejecting Type Annotation

I have a StreamBuilder that is taking data from my bloc component.
However it keeps rejecting my type annotation AsyncSnapshot<List<int>> snapshot and only accepts dynamic as a type AsyncSnapshot<List<dynamic>> snapshot. Yet in the examples i've viewed they do have type annotaions with no complaints.
Here is my stream creation.
Widget buildList(StoriesBloc bloc) {
return StreamBuilder(
stream: bloc.topIds,
builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
if (!snapshot.hasData) {
return Text("Still Waiting for Ids to fetch");
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, int index) {
return Text('${snapshot.data[index]}');
},
);
},
);
}
Here is the VSCode error generated.
What could i be doing wrong ?
Turns out my bloc.topIds result type was not of type List<int>.
Observable<List> get topIds => _topIds.stream;
So i simply changed it to fulfill the required type.
Observable<List<int>> get topIds => _topIds.stream;
And that fixed the issue. Hope it helps someone else.