无法将参数类型“字符串”分配给参数类型“数据”

时间:2021-05-22 15:12:03

标签: flutter

我在 flutter 中创建了一个 listview 来显示一组已经存在于数据库中的图像,如下所示:

enter image description here

照片正确来自数据库,但我添加了一个按钮来从手机获取其他图片并将它们添加到同一个 listView。

获取以下代码中的错误:


 onTap: () async {
            PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery, imageQuality: 50);
                                                                        setState(() {
                                                                          if (pickedFile != null) {
                                                                            filteredUsers.add(pickedFile.path);
                                                                          }
                                                                        });

                                                                      }

The argument type 'String' can't be assigned to the parameter type 'FlowerdataImage'.

完整代码:

class _update extends State<UpdateItem> {
  @override
  void initState() {
    super.initState();

    GetImageOfTopic().then((value) {
      setState(() {
        users.addAll(value);
        filteredUsers = users;
      });
    });
  }
  List<FlowerdataImage> users = List();
  List<FlowerdataImage> filteredUsers = List();
  List<String> photoPaths = [];
  final picker = ImagePicker();

  String apiURL;
  Future<List<FlowerdataImage>> GetImage() async {
    apiURL = '*****************.php';
    var response = await http.post(Uri.parse(apiURL));
    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();
      List<FlowerdataImage> listOfFruits = items.map<FlowerdataImage>((json) {
        return FlowerdataImage.fromJson(json);
      }).toList();

      return listOfFruits;

      throw Exception('Failed to load data from Server.');
    }
  }

  @override
  Widget build(BuildContext context) {


    return new Scaffold(

      body: Form(

          child: new Center(
            child: ListView(
                    children: <Widget>[
                      Card(
                        child: new Column(
                          children: <Widget>[
                            Container(

                                  child: Center(
                                    child: Column(
                                      children: <Widget>[


                                     SingleChildScrollView(
                                            child: SizedBox(
                                              child: Column(

                                                children: <Widget>[
                                                  Padding(
                                                    padding:
                                                    EdgeInsets.only(right: 8.0, top: 8.0, ),
                                                    child: SizedBox(
                                                      height: 140,
                                                      child: Row(
                                                        children: [
                                                          Expanded(
                                                            child: Container(

                                                              child: ListView.builder(
                                                                itemCount: filteredUsers.length,
                                                                scrollDirection: Axis.horizontal,
                                                                itemBuilder: (BuildContextcontext, int index) {
                                                                  return InkWell(
                                                                    child: Padding(
                                                                        padding: EdgeInsets.only(top: 8.0, bottom: 8.0, left: 8.0, right: 8.0),
                                                                        child:
                                                                        Container(
                                                                            height: 140, width: 140,
                                                                            child:
                                                                            ClipRRect(
                                                                              borderRadius:
                                                                              BorderRadius.circular(8),
                                                                              child:
                                                                              AspectRatio(
                                                                                aspectRatio:
                                                                                1.2,
                                                                                child:
                                                                               Image.network(
                                                                                      filteredUsers[index].ImageURL.toString(),
                                                                                      width: double.infinity,
                                                                                      height: 400,
                                                                                      fit: BoxFit.cover,
                                                                                    ),

                                                                              ),
                                                                            ))),

                                                                  );
                                                                },
                                                              ),
                                                            ),
                                                          ),
                                                          Padding(
                                                            padding:
                                                            const EdgeInsets.only(left: 10, right: 10),
                                                            child: Container(
                                                                width: 50,
                                                                height: MediaQuery.of(
                                                                    context)
                                                                    .size
                                                                    .height,
                                                                decoration:
                                                                BoxDecoration(
                                                                  color:
                                                                  Theme.of(context)
                                                                      .accentColor,
                                                                  borderRadius:
                                                                  BorderRadius
                                                                      .circular(8),
                                                                ),
                                                                child: Material(
                                                                  color: CustomColors.Background,


                                                                  borderRadius:
                                                                  BorderRadius
                                                                      .circular(8),
                                                                  child: InkWell(
                                                                      borderRadius:
                                                                      BorderRadius
                                                                          .circular(
                                                                          8),
                                                                      child: Icon(
                                                                        Icons.add_a_photo,
                                                                        color: Theme.of(context).floatingActionButtonTheme.foregroundColor,
                                                                      ),
                                                                      onTap: () async {
            PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery, imageQuality: 50);
                                                                        setState(() {
                                                                          if (pickedFile != null) {
                                                                            filteredUsers.add(pickedFile.path);
                                                                          }
                                                                        });

                                                                      }),
                                                                )),
                                                          )
                                                        ],
                                                      ),
                                                    ),
                                                  ),

                                                ],
                                              ),
                                            ),
                                          ),




                                      ],
                                    ),
                                  ),
                                ),





                          ],
                        ),
                      ),



                    ],
                  ),
          )),
    );
  }
}


class FlowerdataImage {
  int id;

  String ImageURL;

  FlowerdataImage({
    this.id,

    this.ImageURL,

  });

  factory FlowerdataImage.fromJson(Map<String, dynamic> json) {
    return FlowerdataImage(
      id: json['id'],

      ImageURL: json['image'].toString(),

    );
  }
}

我试图解决这个问题很长时间,但没有奏效。

1 个答案:

答案 0 :(得分:2)

您正在尝试将 String 添加到 FlowerdataImage 的集合中。这是不可能的。

在您拥有的行中:

filteredUsers.add(pickedFile.path);

您需要将 path 转换为有效的 FlowerdataImage 对象,以便适应 List

filteredUsers.add(FlowerdataImage(imageURL: pickedFile.path, isLocal: true));

还将您的课程更新为:

class FlowerdataImage {
  int id;
  bool isLocal;
  String ImageURL;

  FlowerdataImage({
    this.id,
    this.ImageURL,
    this.isLocal = false,

  });

  factory FlowerdataImage.fromJson(Map<String, dynamic> json) {
    return FlowerdataImage(
      id: json['id'],

      ImageURL: json['image'].toString(),

    );
  }
}

然后,在您的 build 方法更新为:

filteredUsers[index].isLocal ? 
Image.file(File(filteredUsers[index].ImageURL),
   width: double.infinity,
   height: 400,
   fit: BoxFit.cover,
) : Image.network(
   filteredUsers[index].ImageURL.toString(),
   width: double.infinity,
   height: 400,
   fit: BoxFit.cover,
                                                                                    ),

但是,请记住,您应该有一种方法来区分本地资产(选择的资产)和远程资产(来自 API),以便您可以相应地使用 Image.networkImage.file