Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Answer

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();
            },
          ),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...