这是问题,我正面临着图像选择器
The argument type 'Future<File> (where File is defined in C:\Users\RIDDHI\AndroidStudioProjects\flutter\bin\cache\pkg\sky_engine\lib\io\file.dart)' can't be assigned to the parameter type 'Future<File> (where File is defined in C:\Users\RIDDHI\AndroidStudioProjects\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)'.
错误所在的我的代码
import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:admin_app/db/category.dart';
import 'package:admin_app/db/brand.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:image_picker/image_picker.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:firebase_storage/firebase_storage.dart';
class AddProduct extends StatefulWidget {
@override
_AddProductState createState() => _AddProductState();
}
class _AddProductState extends State<AddProduct> {
CategoryService _categoryService = CategoryService();
BrandService _brandService = BrandService();
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController productNameController = TextEditingController();
TextEditingController quatityController = TextEditingController();
List<DocumentSnapshot> brands = <DocumentSnapshot>[];
List<DocumentSnapshot> categories = <DocumentSnapshot>[];
List<DropdownMenuItem<String>> categoriesDropDown = <DropdownMenuItem<String>>[];
List<DropdownMenuItem<String>> brandsDropDown = <DropdownMenuItem<String>>[];
String _currentCategory;
String _currentBrand;
Color white = Colors.white;
Color black = Colors.black;
Color grey = Colors.grey;
Color red = Colors.red;
List<String> selectedSizes = <String>[];
File _image1;
File _image2;
File _image3;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.1,
backgroundColor: white,
leading: Icon(Icons.close, color: black,),
title: Text("add product", style: TextStyle(color: black),),
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide: BorderSide(color: grey.withOpacity(0.5), width: 2.5),
onPressed: (){
_selectImage(ImagePicker.pickImage(source: ImageSource.gallery), 1);
},
child: _displayChild1()
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide: BorderSide(color: grey.withOpacity(0.5), width: 2.5),
onPressed: (){
_selectImage(ImagePicker.pickImage(source: ImageSource.gallery), 2);
},
child: _displayChild2()
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide: BorderSide(color: grey.withOpacity(0.5), width: 2.5),
onPressed: (){
_selectImage(ImagePicker.pickImage(source: ImageSource.gallery), 3);
},
child: _displayChild3(),
),
),
),
],
),
void _selectImage(Future<File> pickImage, int imageNumber) async{
File tempImg = await pickImage;
switch(imageNumber){
case 1: setState(() => _image1 = tempImg);
break;
case 2: setState(() => _image2 = tempImg);
break;
case 3: setState(() => _image3 = tempImg);
break;
}
}
Widget _displayChild1() {
if(_image1 == null){
return Padding(
padding: const EdgeInsets.fromLTRB(14, 70, 14, 70),
child: new Icon(Icons.add, color: grey,),
);
}else{
return Image.file(_image1, fit: BoxFit.fill, width: double.infinity,);
}
}
Widget _displayChild2() {
if(_image2 == null){
return Padding(
padding: const EdgeInsets.fromLTRB(14, 70, 14, 70),
child: new Icon(Icons.add, color: grey,),
);
}else{
return Image.file(_image2, fit: BoxFit.fill, width: double.infinity,);
}
}
Widget _displayChild3() {
if(_image3 == null){
return Padding(
padding: const EdgeInsets.fromLTRB(14, 70, 14, 70),
child: new Icon(Icons.add, color: grey,),
);
}else{
return Image.file(_image3, fit: BoxFit.fill, width: double.infinity,);
}
}
错误主要是内联ImagePicker.pickImage(source: ImageSource.gallery)
我在pubspec.yaml中添加了图像选择器,并导入了“ package:image_picker / image_picker.dart” 如果有人可以提供帮助,请。我读过不同的解决方案,但没有一个起作用
答案 0 :(得分:2)
更改/删除您的“ dart:html”导入,以导入“ dart:io”;可以解决您遇到的问题。像这样:
import 'dart:io';
不是import 'dart:html';
现在应该工作:)
答案 1 :(得分:1)
请为import 'dart:html';
删除您最近导入的File
并使用此导入:
import 'dart:io';
代替此import 'dart:html';
希望这会有所帮助:D