Abo_nosa
Abo_nosa

Reputation: 233

Passing image from one screen to another - Flutter

I'm using image picker to get the image , and once it's uploaded , I'd like to pass and display it on a different screen. now I'm getting this error

type 'String' is not a subtype of type 'File'

First screen :

void _openCamera(BuildContext context) async {
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.camera,
    );
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => second(image: pickedFile)));
  }

second screen :

class second extends StatelessWidget {
  const second({
    Key? key,
    this.image,
  }) : super(key: key);

  final image;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: SingleChildScrollView(
          child: Container(
              padding: EdgeInsets.all(16),
              child: Card(child: Image.file((image!.path))))),
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
      ),
      floatingActionButton: const FloatingActionButton(onPressed: null),
    );
  }
}

Upvotes: 3

Views: 3593

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63559

Use XFile? with pickImage().

  void _openCamera(BuildContext context) async {
    final XFile? pickedFile = await ImagePicker().pickImage(
      source: ImageSource.camera,
    );

    if (pickedFile == null) return;
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (_) => second(image: pickedFile)));
  }

And to use it

 const second({
    Key? key,
    required this.image,
  }) : super(key: key);

  final XFile image;
 ......

 child: Image.file(File(image.path)),

Upvotes: 2

Related Questions