Home > Mobile >  A value of type 'XFile' can't be assigned to a variable of type 'File'
A value of type 'XFile' can't be assigned to a variable of type 'File'

Time:04-05

i'm trying to create an app in flutter, i'm using image_picker in the following code for app profile:

  Widget editButton() {
    return IconButton(
      icon: Icon(Icons.edit),
      color: Colors.white,
      onPressed: () async {
        File image = await ImagePicker.pickImage(source: ImageSource.gallery);

        await locator.get<UserController>().uploadProfilePicture(image);

        setState(() {
          profilePicture();
        });
      },
    );
  }

i'm getting 2 errors: -A value of type 'XFile' can't be assigned to a variable of type 'File'; -Instance member 'pickImage' can't be accessed using static access.

what'wrong?

CodePudding user response:

To remove the error: Instance member 'pickImage' can't be accessed using static access. You have to create an instace of the class ImagePicker, like this:

  Widget editButton() {
    return IconButton(
      icon: Icon(Icons.edit),
      color: Colors.white,
      onPressed: () async {
        final ImagePicker _picker = ImagePicker(); // <--- here
        XFile image = await _picker.pickImage(source: ImageSource.gallery);  // You have to also use _picker instance ---> _picker.pickImage
 
        await locator.get<UserController>().uploadProfilePicture(image);

        setState(() {
          profilePicture();
        });
      },
    );
  }

CodePudding user response:

I post my full code

class Profile extends StatefulWidget {
  @override
  _ProfileState createState() => _ProfileState();
}


class _ProfileState extends State<Profile> {Widget build(BuildContext context) {

  return Scaffold(
    body: Center(
      child:
      Container(
        height: 200,
        width: 100,
        color:  Colors.yellow,
      ),
    ),
  );
}
  final AuthService _auth = AuthService();
  UserModel _currentUser = locator.get<UserController>().currentUser;
  List<double> lastSixWeeks = [0, 0, 0, 0, 0, 0];

  Future<void> refresh() async {
    await locator.get<UserController>().setLatestWeek();
    setState(() {
      weeklyScores();
      usernameColumn();
    });
  }

  Future<void> getLastSixWeeksData() async {
    lastSixWeeks = await locator.get<UserController>().getLastSixUserScores();
  }



  Widget weeklyScores() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey.withOpacity(0.5),
            width: 1.5,
          ),
        ),
      ),
      child: Padding(
        padding: EdgeInsets.only(top: 5, bottom: 5),
        child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('This', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600)),
                    Text('Week', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600)),
                  ],
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(_currentUser.weekScore.toString(),
                        style: TextStyle(
                            fontSize: 26.0, fontWeight: FontWeight.w500)),
                    Text('Correct'),
                  ],
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(_currentUser.weekTotal.toString(),
                        style: TextStyle(
                            fontSize: 26.0, fontWeight: FontWeight.w500)),
                    Text('Answered'),
                  ],
                ),
                Text(
                    ((_currentUser.weekScore / _currentUser.weekTotal) * 100)
                            .toStringAsFixed(0)  
                        '%',
                    style:
                        TextStyle(fontSize: 34.0, fontWeight: FontWeight.w500)),
              ],
            ),
        ),
      );
  }



  Widget signOutButton() {
    return Padding(
      padding: EdgeInsets.fromLTRB(16.0, 5.0, 16.0, 0.0),
      child: FlatButton(
        child: Container(
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Icon(Icons.exit_to_app),
                  SizedBox(width: 10),
                  Text('sign out')
                ],
              ),
              Icon(Icons.arrow_forward_ios, size: 15.0),
            ],
          ),
        ),
        onPressed: () {
          _auth.signOut();
        },
      ),
    );
  }

  Widget userHeader() {
    return Container(
      height: 280,
      decoration: BoxDecoration(
          color: Color(0xff01B4BC),
          border: Border(
              bottom: BorderSide(
            color: Colors.grey.withOpacity(0.5),
            width: 1.5,
          ))),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Align(
            alignment: Alignment.centerRight,
            child: Padding(
                padding: EdgeInsets.fromLTRB(0.0, 40.0, 15.0, 0.0),
                child: editButton()),
          ),
          profilePicture(),
          usernameColumn(),
        ],
      ),
    );
  }

  Widget editButton() {
    return IconButton(
      icon: Icon(Icons.edit),
      color: Colors.white,
      onPressed: () async {
        final ImagePicker _picker = ImagePicker();
        XFile image = await _picker.pickImage(source: ImageSource.gallery);

        await locator.get<UserController>().uploadProfilePicture(image);

        setState(() {
          profilePicture();
        });
      },
    );
  }```
  • Related