Ad

The Argument Type 'File' Can't Be Assigned To The Parameter Type 'File '

- 1 answer

Hey guys I am relatively new to flutter.I don't understand the meaning of this error. The argument types are the same . I am getting error in the " Provider.of(context, listen: false).addPlace( _titleController.text, _pickedImage!);"

This is my screen where I am receiving an error:

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

this is the addplace function:

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

Thanks in advance for the help

Ad

Answer

The code for _AddPlaceScreenState uses File from dart:html. dart:html's File class is not the same as dart:io's File class, which your addPlace function presumably expects.

In general, if you get a confusing error message:

The argument type 'SomeType' can't be assigned to the parameter type 'SomeType'

it means that you have different classes with the same name.

Ad
source: stackoverflow.com
Ad