第一个 Flutter 应用程序,我遇到了一个错误
First Flutter Application and i got stuck with an error
我试图从图库中获取图像并将其更新为 UI。但是在执行此操作时,它在第 56 行的 profile.dart 文件中显示错误。
错误发生在_image。
The argument type 'File(where File is defined in
D:\Softwares\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)'
can't be assigned to the parameter type 'File(where File is defined in
D:\Softwares\flutter\bin\caches\pkg\sky_engine\lib\io\file.dart)'
完整代码。
Main.dart
import 'package:flutter/material.dart';
import 'package:udharibook/Screens/SplashScreen.dart';
import 'package:udharibook/Screens/UserProfile.dart';
void main(){
runApp(
MaterialApp(
title: 'Udhari Book',
home:UserProfile(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
backgroundColor: Color.fromRGBO(242, 242, 242, 1.0)
),
)
);
}
个人资料页面
import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path/path.dart';
class UserProfile extends StatefulWidget {
@override
_UserProfileState createState() => _UserProfileState();
}
class _UserProfileState extends State<UserProfile> {
File _image;
TextEditingController nameController =TextEditingController()..text='Mehul Jain';
TextEditingController mobileController =TextEditingController()..text='8856061841';
TextEditingController emailController =TextEditingController()..text='mehuljain3698@gmail.com';
TextEditingController addressController =TextEditingController()..text='Maharashtra';
@override
Widget build(BuildContext context) {
Future getImage() async{
var image=await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image=image as File;
print('Image Path $_image');
});
}
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('User Profile'),
backgroundColor: Color.fromRGBO(162, 42, 43, 1.0),
),
body: Builder(
builder: (context) => Container(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: CircleAvatar(
radius: 50,
backgroundColor: Color.fromRGBO(162, 42, 43, 1.0),
child: ClipOval(
child: SizedBox(
height: 180.0,
width: 180.0,
child:_image!=null?
Image.file(_image,fit: BoxFit.fill):
Image.network(
'https://randomwordgenerator.com/img/picture-generator/55e4d5464f5ba914f1dc8460962e33791c3ad6e04e5074417d2d73dc934bcd_640.jpg',
fit: BoxFit.fill,
)),
),
),
),
Padding(
padding: EdgeInsets.only(top: 60.0),
child: IconButton(
icon: Icon(Icons.camera_alt), onPressed: () {
getImage();
}),
)
],
),
Padding(
padding: EdgeInsets.only(top:20.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: nameController,
decoration: InputDecoration(
labelText: 'Full Name',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: mobileController,
enabled: false,
keyboardType:TextInputType.phone,
decoration: InputDecoration(
labelText: 'Mobile Number',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email (Optional)',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0,bottom: 30.0),
child: TextField(
maxLines: 3,
maxLengthEnforced: true,
controller: addressController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Address (Optional)',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width:130.0,
height: 50.0,
child:RaisedButton(
color: Color.fromRGBO(162, 42, 43, 1.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)
),
onPressed: (){
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Text(
'Save',
style: TextStyle(
color:Colors.white,fontSize: 22.0,fontFamily: 'Exo2'
),
),
)),
SizedBox(
width:130.0,
height:50.0,
child:RaisedButton(
color: Color.fromRGBO(162, 42, 43, 1.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)
),
onPressed: (){
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Text(
'Cancel',
style: TextStyle(
color:Colors.white,fontSize: 22.0,fontFamily: 'Exo2'
),
),
))
],
)
],
),
),
),
);
}
}
欢迎使用 Flutter。很抱歉您遇到此错误!相信我,Flutter 真的是用户友好的,很快你就会掌握它的窍门:)
所以看起来这个错误是由于 2 个或更多使用 File 的导入中的冲突而出现的。看看这个线程:
我试图从图库中获取图像并将其更新为 UI。但是在执行此操作时,它在第 56 行的 profile.dart 文件中显示错误。
错误发生在_image。
The argument type 'File(where File is defined in D:\Softwares\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)' can't be assigned to the parameter type 'File(where File is defined in D:\Softwares\flutter\bin\caches\pkg\sky_engine\lib\io\file.dart)'
完整代码。
Main.dart
import 'package:flutter/material.dart';
import 'package:udharibook/Screens/SplashScreen.dart';
import 'package:udharibook/Screens/UserProfile.dart';
void main(){
runApp(
MaterialApp(
title: 'Udhari Book',
home:UserProfile(),
debugShowCheckedModeBanner: false,
theme: ThemeData(
backgroundColor: Color.fromRGBO(242, 242, 242, 1.0)
),
)
);
}
个人资料页面
import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path/path.dart';
class UserProfile extends StatefulWidget {
@override
_UserProfileState createState() => _UserProfileState();
}
class _UserProfileState extends State<UserProfile> {
File _image;
TextEditingController nameController =TextEditingController()..text='Mehul Jain';
TextEditingController mobileController =TextEditingController()..text='8856061841';
TextEditingController emailController =TextEditingController()..text='mehuljain3698@gmail.com';
TextEditingController addressController =TextEditingController()..text='Maharashtra';
@override
Widget build(BuildContext context) {
Future getImage() async{
var image=await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image=image as File;
print('Image Path $_image');
});
}
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('User Profile'),
backgroundColor: Color.fromRGBO(162, 42, 43, 1.0),
),
body: Builder(
builder: (context) => Container(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: CircleAvatar(
radius: 50,
backgroundColor: Color.fromRGBO(162, 42, 43, 1.0),
child: ClipOval(
child: SizedBox(
height: 180.0,
width: 180.0,
child:_image!=null?
Image.file(_image,fit: BoxFit.fill):
Image.network(
'https://randomwordgenerator.com/img/picture-generator/55e4d5464f5ba914f1dc8460962e33791c3ad6e04e5074417d2d73dc934bcd_640.jpg',
fit: BoxFit.fill,
)),
),
),
),
Padding(
padding: EdgeInsets.only(top: 60.0),
child: IconButton(
icon: Icon(Icons.camera_alt), onPressed: () {
getImage();
}),
)
],
),
Padding(
padding: EdgeInsets.only(top:20.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: nameController,
decoration: InputDecoration(
labelText: 'Full Name',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: mobileController,
enabled: false,
keyboardType:TextInputType.phone,
decoration: InputDecoration(
labelText: 'Mobile Number',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0),
child:SizedBox(
height: 40.0,
child: TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email (Optional)',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
)),
Padding(
padding: EdgeInsets.only(top:15.0,left: 10.0,right: 10.0,bottom: 30.0),
child: TextField(
maxLines: 3,
maxLengthEnforced: true,
controller: addressController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Address (Optional)',
labelStyle:
TextStyle(fontFamily: 'Exo2', color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
borderSide: BorderSide(
color:
Color.fromRGBO(162, 42, 43, 1.0)
)
)
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width:130.0,
height: 50.0,
child:RaisedButton(
color: Color.fromRGBO(162, 42, 43, 1.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)
),
onPressed: (){
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Text(
'Save',
style: TextStyle(
color:Colors.white,fontSize: 22.0,fontFamily: 'Exo2'
),
),
)),
SizedBox(
width:130.0,
height:50.0,
child:RaisedButton(
color: Color.fromRGBO(162, 42, 43, 1.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0)
),
onPressed: (){
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Text(
'Cancel',
style: TextStyle(
color:Colors.white,fontSize: 22.0,fontFamily: 'Exo2'
),
),
))
],
)
],
),
),
),
);
}
}
欢迎使用 Flutter。很抱歉您遇到此错误!相信我,Flutter 真的是用户友好的,很快你就会掌握它的窍门:)
所以看起来这个错误是由于 2 个或更多使用 File 的导入中的冲突而出现的。看看这个线程: