Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

unable to use .toList with Data from MongoDB using flutter

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.

question from:https://stackoverflow.com/questions/65893041/unable-to-use-tolist-with-data-from-mongodb-using-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 {
  Welcome({
    this.id,
    this.name,
  });

  int id;
  String name;
}

This would be your Azkar class

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');

    var list = await coll.find().toList();
    List<User> users = [];
    for (var v in list) {
        User user = User(
            id: v.id,
            name: v.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();
            },
          ),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...