Home > OS >  Flutter package lateintialization error - '_image' has not been initialized
Flutter package lateintialization error - '_image' has not been initialized

Time:02-04

I'm trying to make the image_editor_pro package in flutter to work using their example, but when I run the app, I get a lateintialization error '_image' has not been initialized , I tried replacing late with ? , but then I got another error since File type can't allow null values

The code :

import 'dart:io';
import 'package:image_editor_pro/image_editor_pro.dart';
import 'package:firexcode/firexcode.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HomePage().xMaterialApp();
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late File _image;

  Future<void> getimageditor() =>
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return ImageEditorPro(
          appBarColor: Colors.black87,
          bottomBarColor: Colors.black87,
          pathSave: null,
        );
      })).then((geteditimage) {
        if (geteditimage != null) {
          setState(() {
            _image = geteditimage;
          });
        }
      }).catchError((er) {
        print(er);
      });

  @override
  Widget build(BuildContext context) {
    return condition(
            condtion: _image == null,
            isTrue: 'Open Editor'.text().xRaisedButton(
              onPressed: () {
                getimageditor();
              },
            ).toCenter(),
            isFalse:
                _image == null ? Container() : Image.file(_image).toCenter())
        .xScaffold(
            appBar: 'Image Editor Pro example'.xTextColorWhite().xAppBar(),
            floatingActionButton:
                Icons.add.xIcons().xFloationActiobButton(color: Colors.red));
  }
}

Widget condition(
    {required bool condtion, required Widget isTrue, required Widget isFalse}) {
  return condtion ? isTrue : isFalse;
}

CodePudding user response:

Instead of using late File _image; you can use File? _image; which makes the _image variable nullable with default value null. Make sure to have proper null checks like (_image != null ) or (image?.method)

CodePudding user response:

You could use a boolean to check if _image has been initialized:

class _HomePageState extends State<HomePage> {
  late File _image;
  bool _isImageInitialized = false;

  Future<void> getimageditor() =>
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return ImageEditorPro(
          appBarColor: Colors.black87,
          bottomBarColor: Colors.black87,
          pathSave: null,
        );
      })).then((geteditimage) {
        if (geteditimage != null) {
          setState(() {
            _image = geteditimage;
            _isImageInitialized = true;
          });
        }
      }).catchError((er) {
        print(er);
      });

  @override
  Widget build(BuildContext context) {
    return condition(
            condtion: _isImageInitialized == false,
            isTrue: 'Open Editor'.text().xRaisedButton(
              onPressed: () {
                getimageditor();
              },
            ).toCenter(),
            isFalse:
                Image.file(_image).toCenter())
        .xScaffold(
            appBar: 'Image Editor Pro example'.xTextColorWhite().xAppBar(),
            floatingActionButton:
                Icons.add.xIcons().xFloationActiobButton(color: Colors.red));
  }
}
  •  Tags:  
  • Related