unable to use .toList with Data from MongoDB using flutter
Solution 1
This package is not worth it. I solved this issue by moving out this part of code on the backend side (NodeJS) in the cloud and just getting what I need with an HTTP request.
Solution 2
Instead of returning data in List<Map<String, dynamic>>
, create a class for your data. Suppose your data gives us a list of users. Then
class User {
User({
this.id,
this.name,
});
int id;
String name;
}
This would be your Azkar
class
class Azkar {
getAzkar() async {
final db = await Db.create(
'mongodb+srv://Adham:<password>@cluster0.nm0lg.mongodb.net/<db>retryWrites=true&w=majority');
await db.open();
print('Connected to database');
final coll = db.collection('zekrs');
final zekrsList = await coll.find().toList();
List<User> users = [];
for (var item in zekrsList) {
final user = User(
id: item['id'],
name: item['name'],
);
users.add(user);
}
return users;
}
}
You should do something like this.
FutureBuilder(
future: getAzkar(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(8),
child: Column(
children: [
Text("Name = ${snapshot.data[index].name}"),
Text("Id = ${snapshot.data[index].id}"),
],
),
);
});
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
Adham Fouad Hussein
👋🏽 Hi, my name is Adham 🎓 Soon to be grad with a High School Diploma, Marketing. (Aug 2023) 🌇 Cairo Native, with a passion for creating software and developing solutions that will grow your business 📚 Made Websites, Management systems for corporations and cross platform apps for several clients as well as contributing to make open source apps. 💻 As for my future, I hope to one day work as Full stack software developer and work with a diverse team. 🏔 In my free time, you can find me swimming for the Egyptian Gold swimming team at Wadi Degla. 💪🏽 Problem solving, Backend development, Frontend development, Building APIs, Leading teams, and I am a Fast learner.
Updated on December 27, 2022Comments
-
Adham Fouad Hussein over 1 year
Sorry if it's a stupid question I am beginner in Flutter and MongoDB Here is my code to return collection data btw this is the only time I use Mongo_Dart all other operations done using JS on heroku
class Azkar { getAzkar() async { var db = await Db.create( 'mongodb+srv://Adham:<password>@cluster0.nm0lg.mongodb.net/<db>retryWrites=true&w=majority'); await db.open(); print('Connected to database'); DbCollection coll = db.collection('zekrs'); return await coll.find().toList(); } }
It is working and I am able to print returned data from another class it is List<Map<String, dynamic>> I want to know how should I use it to generate ListTile with all data.
-
Sravan Kumar over 3 yearswould be better if you return in json
-
Adham Fouad Hussein about 3 yearsYou can't return in json it's either string or list
-
-
Adham Fouad Hussein about 3 yearsawait coll.find().toList(); what should I change it for then, thank you so much for the effort
-
Sravan Kumar about 3 yearsyeah, for that.... final list = await coll.find().toList(); And use list further wherever I used col1 in the answer. Pls accept my answer and give an upvote if my answer worked
-
Adham Fouad Hussein about 3 yearsfor (var v in list) { User user = User( id: v.id, // error // name: v.name) // error The getter 'id' isn't defined for the type 'Map<String, dynamic>'. Try importing the library that defines 'id', correcting the name to the name of an existing getter, or defining a getter or field named 'id'.
-
richaux almost 2 yearsThe way to access the Map properties is e.g.
v['id']
rather thanv.id