【问题标题】:flutter map Instance of "configuration firebaseflutter map Instance of \"configuration firebase
【发布时间】:2022-12-01 18:00:39
【问题描述】:

hello I am trying to get list of data but an instance of configurations is printed it works with listview but I want it as listView.builder

here is my code : `

 class Configurations {
      final List<dynamic> brands;
    
      Configurations({
        required this.brands,
      });
    
      Map<String, dynamic> toJson() => {
            'brands': brands,
          };
    
      static Configurations fromJson(Map<String, dynamic> json) => Configurations(
            brands: json['brands'],
          );
    }

  Stream<List<Configurations>> readConfi() {
    return FirebaseFirestore.instance
        .collection('Configurations')
        .snapshots()
        .map((snapshot) => snapshot.docs
            .map((doc) => Configurations.fromJson(doc.data()))
            .toList());
  }

Expanded(
              child: StreamBuilder<List<Configurations>>(
            stream: readConfi(),
            builder: (BuildContext context,
                AsyncSnapshot<List<Configurations>> snapshot) {
              if (snapshot.hasData) {
                final brands = snapshot.data!;
                return ListView.builder(
                  itemCount: brands.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Column(
                      children: [Text("${brands[index]}")],
                    );
                  },
                );
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ))

` here is an image of data I want to get

【问题讨论】:

    标签: flutter firebase


    【解决方案1】:

    This is because you are calling brands[index], this is returning the object of Configuration. If you want to get brands list use the following.

    Text("${brands[index].brands}")
    

    Or you can change the variable name from

    final brands = snapshot.data!;
    

    to

    final configuration = snapshot.data!;
    

    then use

    Text("${configuration[index].brands}")
    

    Furthermore if you want to iterate the list of brands.

    Text("${configuration[index].brands.join(', ')}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2021-12-15
      • 2023-02-02
      • 2023-04-08
      • 2018-04-11
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多