'package:flutter / src / painting / _network_image_io.dart':失败的断言:第25行pos 14:'url!= null':不正确

时间:2020-11-11 10:02:54

标签: firebase flutter listview storage

我想在列表视图中显示Firestore中的图像。因此,这是我的编码。

我的列表视图类。

import 'package:carousel_pro/carousel_pro.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:fyp/shared/Loading.dart';

class ListTask extends StatefulWidget {
  @override
  _ListTaskState createState() => _ListTaskState();
}


final FirebaseAuth auth = FirebaseAuth.instance;
Stream<QuerySnapshot> getUserRd(BuildContext context) async* {
  final FirebaseUser rd = await auth.currentUser();
  yield* Firestore.instance.collection("Task").where('uid',isEqualTo: rd.uid).snapshots();
}
List<NetworkImage> _listOfImages = <NetworkImage>[];
class _ListTaskState extends State<ListTask> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: StreamBuilder(
          stream: getUserRd(context),
          builder: (context, snapshot){
            if (snapshot.hasError || !snapshot.hasData) {
              return Loading();
            } else{
              return ListView.builder(
                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (BuildContext context, int index){
                    _listOfImages =[];
                    for(int i =0; i <snapshot.data.documents[index].data['urls'].length; i++){
                      _listOfImages.add(NetworkImage(snapshot.data.documents[index].data['urls'[i]]));  //show this line was error
                    }
                    final ba = snapshot.data.documents[index];
                    return Card(
                        child:ListTile(
                          title: Container(
                            alignment: Alignment.centerLeft,
                            child: Column(
                              children: <Widget>[
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Text(ba['sumberAduan']),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Text(ba['noAduan']),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Text(ba['kategori']),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Text(ba['verified']),
                                ),
                                Column(
                                  children: [
                                      Container(
                                        margin: EdgeInsets.all(10.0),
                                        height: 200,
                                        decoration: BoxDecoration(
                                          color: Colors.white
                                        ),
                                         width: MediaQuery.of(context).size.width,
                                        child: Carousel(
                                          boxFit: BoxFit.cover,
                                          images: _listOfImages,
                                          autoplay: false,
                                          indicatorBgPadding: 5.0,
                                          dotPosition: DotPosition.bottomCenter,
                                          animationCurve: Curves.fastLinearToSlowEaseIn,
                                          animationDuration: Duration(milliseconds: 2000),
                                        ),
                                      )
                                  ],
                                )
                              ],
                            ),
                          ),
                        )
                    );
                  });
               }
          }),
    );
  }
}

这是我从firestore收集的文件。我想显示网址中的图片。

task collection

我尝试了很多方法来显示图像,但不幸的是对我不起作用。那我想念那里吗?有人请帮助我。

2 个答案:

答案 0 :(得分:0)

此:

data['urls'[i]]

可能应该是

data['urls'][i]

答案 1 :(得分:0)

尝试在您的 itemBuilder

中执行此操作
  final List<NetworkImage> _listOfImages = snapshot.data.documents[index].data['urls']
    .map((String e) => NetworkImage(e))
    .toList();

map遍历URL列表并将其转换为NetworkImages