How to solve: type 'List<dynamic>' is not a subtype of type 'String'

953 Views Asked by At

I want to update image from listofTaskNotApprove class which it will pass the object of documentsnapshot into the EditTaskNotApprove. Before I update the image, I need to display specific of image where the user will be select specific info from listofTaskNotApprove. The problem is how to display the current index of image into the new screen?

ListOfTaskNotAccepted class.

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

Here is EditTask class which I need to display current index of image that selected by user.

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

the image that need to display for update image This is how I want to display image in class EditTask when the user want to update information from ListOfTaskNotApprove

The error show that "type 'List' is not a subtype of type 'String'" Can someone help me? because I had tried many method to solve this problem but it didn't work for me.

2

There are 2 best solutions below

9
On BEST ANSWER

You say:

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

Yes, you can't cast a NetworkImage to a List<NetworkImage>. You probably meant:

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

The thing is your are getting a list of url's from Firebase storage which are Strings but you are adding this strings into a list of type Network Image which is wrong. As a list of string cannot be converted to a list of Network Image.

There are 2 ways to resolve this-

  1. Change your list type to List and then wherever you show this image use list data as an argument for Network Image.

     List<String> urls=new List();
     //suppose list is not empty
     ....
     return NetworkImage(urls[i]);
     ....
    
  2. While adding url's to your list add an Network Image object and directly use the list item while showing images.

     List<NetworkImage> list=new List();
     list.add(NetworkImage('some url'));
     ....
     return list[i];
     ....