当前位置: 首页 > 知识库问答 >
问题:

如何解决:类型列表不是类型字符串的子类型[关闭]

罗金林
2023-03-14

我想从listofTaskNotApprove类更新图像,它将documentsnapshot的对象传递到EditTaskNotApprove。在我更新图像之前,我需要显示特定的图像,用户将从列表中选择特定信息。问题是如何在新屏幕上显示图像的当前索引?

ListofTasKnot接受类。

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/screen/RecordOfficer/EditTaskNotApprove.dart';
import 'package:fyp/shared/Loading.dart';
import 'package:google_fonts/google_fonts.dart';


class ListOfTaskNotAccepted extends StatefulWidget {
  @override
  _ListOfTaskNotAcceptedState createState() => _ListOfTaskNotAcceptedState();
}
final FirebaseAuth auth = FirebaseAuth.instance;
Stream<QuerySnapshot> getUser(BuildContext context) async* {
  final FirebaseUser rd = await auth.currentUser();
  yield* Firestore.instance.collection("Task").where('uid',isEqualTo: rd.uid).where("verified", isEqualTo: 'TidakSah').snapshots();
}
class _ListOfTaskNotAcceptedState extends State<ListOfTaskNotAccepted> {
  List<NetworkImage> _listOfImages = <NetworkImage>[];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Aduan Tidak Diterima"),
          backgroundColor: Colors.redAccent,
        ),
      body: Container(
      child: StreamBuilder(
          stream: getUser(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){
                   DocumentSnapshot da = snapshot.data.documents[index];
                    _listOfImages =[];
                    for(int i =0; i <da['url'].length; i++){
                      _listOfImages.add(NetworkImage(da['url'][i]));
                    }
                    return Card(
                        child:ListTile(
                          title: Container(
                            alignment: Alignment.centerLeft,
                            child: Column(
                              children: <Widget>[
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Row(
                                    children: [
                                      Text("Sumber Aduan: ", style: GoogleFonts.asap(fontWeight: FontWeight.bold)),
                                      Text(da['sumberAduan'], style: GoogleFonts.asap(fontWeight: FontWeight.bold)),
                                    ],
                                  ),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Row(
                                    children: [
                                      Text("Nombor Aduan: ", style: GoogleFonts.lato(fontWeight: FontWeight.bold)),
                                      Text(da['noAduan'], style: GoogleFonts.lato(fontWeight: FontWeight.bold)),
                                    ],
                                  ),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Row(
                                    children: [
                                      Text("Lokasi: ", style: GoogleFonts.lato(fontWeight: FontWeight.bold)),
                                      Text(da['kawasan'] + " " + da['naJalan'], style: GoogleFonts.lato(fontWeight: FontWeight.bold)),
                                    ],
                                  ),
                                ),
                                SizedBox(height: 5.0),
                                Container(alignment: Alignment.centerLeft,
                                  child: Row(
                                    children: [
                                      Text("Kategori: ", style: GoogleFonts.arimo(fontWeight: FontWeight.w500)),
                                      Text(da['kategori'], style: GoogleFonts.arimo(fontWeight: FontWeight.w500)),
                                    ],
                                  ),
                                ),
                                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),
                                      ),
                                    )
                                  ],
                                )
                              ],
                            ),
                          ),
                          subtitle: Container(
                          child: Column(
                            children: [
                              SizedBox(height: 5.0),
                              Container(alignment: Alignment.centerLeft,
                                child: Row(
                                  children: [
                                    Text("Catatan: ", style: GoogleFonts.arimo(fontWeight: FontWeight.w500)),
                                    Text(da['comments'], style: GoogleFonts.arimo(fontWeight: FontWeight.w500)),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                          onTap: () {Navigator.push(context, MaterialPageRoute(builder: (context) => EditTask(da:da)));}
                        )
                    );
                  });
               }
          }),
       )
    );
  }
}

这里是EditTask类,我需要显示用户选择的图像的当前索引。

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



class EditTask extends StatefulWidget {
 final DocumentSnapshot da;

  const EditTask({Key key, this.da}) : super(key: key);

  @override
  _EditTaskState createState() => _EditTaskState(da);
}

class _EditTaskState extends State<EditTask> {
  DocumentSnapshot da;
  _EditTaskState(DocumentSnapshot da){
    this.da = da;
  }
 TextEditingController _noAduan;
 TextEditingController _sumberAduan;
 TextEditingController _kategori;
  DateTime myDateTime = DateTime.now();
 @override
 void initState(){
   super.initState();
   _noAduan = TextEditingController(text: widget.da.data['noAduan']);
   _sumberAduan =TextEditingController(text: widget.da.data['sumberAduan']);
   _kategori = TextEditingController(text: widget.da.data['kategori']);
   myDateTime = (da.data['date']).toDate();
  _listOfImages = NetworkImage(da.data['url']) as List<NetworkImage>; // this line show the error 

}

  List <String> sumber = <String> ['Sistem Aduan MBPJ', 'Sistem Aduan Waze', 'Sistem Aduan Utiliti'];
  List <String> kate = <String> ['Segera', 'Pembaikan Biasa'];
  String kategori;
  String sumberAduan;
  List <NetworkImage> _listOfImages = <NetworkImage>[];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Kemaskini Aduan"),
          backgroundColor: Colors.redAccent,
        ),
    body: Container(
      padding: const EdgeInsets.all(16.0),
      child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              SizedBox(height: 10.0),
              TextFormField(
                decoration:InputDecoration(
                    hintText: myDateTime.toString(),
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(5))),
                onChanged: (value){
                  setState(() {
                    myDateTime = value as DateTime;
                    print(myDateTime);
                  });
                },
              ),
              SizedBox(height: 10.0),
              TextFormField(
                decoration:InputDecoration(
                  border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5))),
                controller: _noAduan,
              ),
              SizedBox(height: 10.0),
              DropdownButtonFormField(
                hint:Text(widget.da.data['sumberAduan']),
                decoration: InputDecoration(
                    prefixIcon: Icon(Icons.perm_contact_calendar),
                    border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
                isExpanded: true,
                value: sumberAduan,
                onChanged: (newValue) {
                  setState(() {
                    sumberAduan = newValue;
                    _sumberAduan.text = sumberAduan;
                  });
                },
                items: sumber.map((sum){
                  return DropdownMenuItem(
                    value: sum,
                    child: new Text(sum),
                  );
                }).toList(),
              ),
              SizedBox(height: 10.0),
              DropdownButtonFormField(
                hint:Text(widget.da.data['kategori']),
                decoration: InputDecoration(
                    prefixIcon: Icon(Icons.perm_contact_calendar),
                    border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
                isExpanded: true,
                value: kategori,
                onChanged: (newValue) {
                  setState(() {
                    kategori = newValue;
                    _kategori.text = kategori;
                  });
                },
                items: kate.map((ka){
                  return DropdownMenuItem(
                    value: ka,
                    child: new Text(ka),
                  );
                }).toList(),
              ),
              Container(
                margin: EdgeInsets.all(10.0),
                height: 200,
                decoration: BoxDecoration(
                    color: Colors.white
                ),
                width: MediaQuery.of(context).size.width,
                child: Carousel(
                  boxFit: BoxFit.cover,
                  autoplay: false,
                  //images: _listOfImages,
                  indicatorBgPadding: 5.0,
                  dotPosition: DotPosition.bottomCenter,
                  animationCurve: Curves.fastLinearToSlowEaseIn,
                  animationDuration: Duration(milliseconds: 2000),
                ),
              )
            ],
          ),
      ),
    )
    );
  }
}

更新图像需要显示的图像这是当用户想要更新ListOfTask中的信息时,我希望在类EditTask中显示图像的方式

错误显示“type‘List’不是type‘String’的子类型”有人能帮我吗?因为我已经尝试了很多方法来解决这个问题,但对我来说不起作用。

共有2个答案

冯亮
2023-03-14

问题是,您正在从Firebase存储获取一个url列表,这些url是字符串,但您将这些字符串添加到一个类型为Network Image的列表中,这是错误的。无法将字符串列表转换为网络映像列表。

有两种方法可以解决此问题-

>

 List<String> urls=new List();
 //suppose list is not empty
 ....
 return NetworkImage(urls[i]);
 ....

在将url添加到列表时,添加一个网络图像对象,并在显示图像时直接使用列表项。

 List<NetworkImage> list=new List();
 list.add(NetworkImage('some url'));
 ....
 return list[i];
 ....
习宸
2023-03-14

你说:

_listOfImages = NetworkImage(da.data['url']) as List<NetworkImage>; // this line show the error 

是的,您不能将NetworkImage转换为List

_listOfImages = [ NetworkImage(da.data['url']) ] as List<NetworkImage>;

 类似资料:
  • 我得到这个错误列表是不是一个类型的子类型字符串在类型铸造,目前我正在发送的形式数据在体内,因为服务器不接受任何原始请求 //服务器响应,表示服务器成功接收并返回状态200

  • 我是颤振初学者。我得到这样的JSON响应时出错。 我一直在论坛上寻找这个问题,我发现是这样的。 但我不懂如何编写函数。这是我的密码。 还有像这样的错误。 我很抱歉问了同样的问题,因为我不明白

  • 我有点被某些情况困住了。 1)

  • 我试图从API获取数据。我得到了错误类型列表不是地图的子类型。我是新来的,不明白我在哪里犯了错误。 这是我的回帖功能: 在我的主屏幕中,我使用了一个,如下所示: 如何解决此错误?提前谢谢。

  • 这是显示类型列表动态不是类型映射字符串动态的子类型“现在当我运行模拟器时,显示”类型列表动态不是类型映射字符串动态的子类型“在我的模拟器上,我如何修复它? 现在我想创建关于查找api id的程序,从我的模拟器中使用文本字段和按钮,当我把一些数字和单击按钮时,会显示该ID的数据或标题,但我只是初学者,所以如果有人认为我的代码是正确的或不正确的或者你有推荐请告诉我 My JSON JSON代码 应用程

  • 我想从Firestore获取会议并将其映射到以下模型中: 从Firestore获取文档,然后在iterable上调用< code>fromJson,但会引发一个异常: 当我进入生成的< code>meeting.g.dart时,就是这一行导致了错误 为了解决此问题,我尝试在模型中从 DateTime 更改为 Timestamp,但随后显示以下生成错误: 你能告诉我你如何解决这个问题吗?有没有另一种