2

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),
    );
  }
}
Abo_nosa
  • 193
  • 2
  • 13

1 Answers1

1

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)),

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56