【问题标题】:The argument type 'File' can't be assigned to the parameter type 'File '参数类型“文件”不能分配给参数类型“文件”
【发布时间】:2022-06-19 20:12:53
【问题描述】:

我对 Flutter 比较陌生。我不明白这个错误的含义。参数类型相同。我在“ Provider.of(context, listen: false).addPlace( _titleController.text, _pickedImage!);"

这是我收到错误的屏幕:

import 'dart:html';

import 'package:flutter/material.dart';
import 'package:great_places_app/Providers.dart/great_places.dart';
import 'package:great_places_app/Widgets/image_inputs.dart';
import 'package:provider/provider.dart';

class AddPlaceScreen extends StatefulWidget {
  const AddPlaceScreen({Key? key}) : super(key: key);
  static const routeName = '/add-place';

  @override
  _AddPlaceScreenState createState() => _AddPlaceScreenState();
}

class _AddPlaceScreenState extends State<AddPlaceScreen> {
  final _titleController = TextEditingController();
  File? _pickedImage;

  void _selectImage(File pickedImage) {
    _pickedImage = pickedImage;
  }

  void _savePlace() {
    if (_titleController.text.isEmpty || _pickedImage == null) {
      return;
    }
    Provider.of<GreatPlaces>(context, listen: false).addPlace(
        _titleController.text,
        _pickedImage!);  //this is where I am recieving the error
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Add a New Place'),
      ),
      body: Column(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween, no need because of expanded
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Expanded(
              child: SingleChildScrollView(
           
            child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  
                  TextField(
                    decoration: InputDecoration(labelText: 'title'),
                    controller: _titleController,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  imageInput(
                      _selectImage), 
                ],
              ),
            ),
          )),
          ElevatedButton.icon(
            onPressed: () {
              Navigator.of(context).pushNamed(AddPlaceScreen
                  .routeName); 
            },
            icon: Icon(Icons.add_location),
            label: Text('Add Place'),
          ),
        ],
      ),
    );
  }
}

这是 addplace 函数:

void addPlace(String title, File image) {
    final newPlace = Place(
        id: DateTime.now().toString(),
        title: title,
        Image: image,
        location: null);
    _items.add(newPlace);
    notifyListeners();
  }

【问题讨论】:

  • 可以添加地方类代码详情吗?
  • _AddPlaceScreenState 的代码使用来自dart:htmlFiledart:htmlFiledart:ioFile 类相同。您的 addPlace 函数是否期望后者?
  • 你能给你提供更多错误:Provider.of(context, listen: false).addPlace( _titleController.text, _pickedImage!);?
  • 成功了,我确实在一个 dart:HTML 的文件中使用了 dart:io 的文件在另一个

标签: flutter flutter-layout flutter-dependencies


【解决方案1】:

_AddPlaceScreenState 的代码使用来自dart:htmlFiledart:html's File classdart:io's File class 不同,您的 addPlace 函数可能期望它。

一般来说,如果您收到令人困惑的错误消息:

参数类型“SomeType”不能分配给参数类型“SomeType”

这意味着你有不同的类同名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2020-12-03
    • 2020-07-08
    • 2022-12-04
    • 2021-01-05
    • 2020-04-01
    • 2018-12-26
    相关资源
    最近更新 更多