Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call final outside class in flutter?

My form bloc is showing the alert "LateInitializationError: Field ‘fileFieldBloc’ has not been initialized". I initialize final InputFieldBloc<PlatformFile?,Object> fileFieldBloc; inside class HomePage extends StatefulWidget which was a stateful widget, but i can’t call fileFieldBloc inside class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin, is there any way for me to initialize it? this is the code:

class HomePage extends StatefulWidget {

  const HomePage({
    Key? key, required this.fileFieldBloc}) : super(key: key);


  final InputFieldBloc<PlatformFile?,Object> fileFieldBloc;

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {

  late final InputFieldBloc<PlatformFile?, Object> fileFieldBloc;

  String _image = 'https://ouch-cdn2.icons8.com/84zU-uvFboh65geJMR5XIHCaNkx-BZ2TahEpE9TpVJM/rs:fit:784:784/czM6Ly9pY29uczgu/b3VjaC1wcm9kLmFz/c2V0cy9wbmcvODU5/L2E1MDk1MmUyLTg1/ZTMtNGU3OC1hYzlh/LWU2NDVmMWRiMjY0/OS5wbmc.png';
  late AnimationController loadingController;
  File? _file;
  PlatformFile? _platformFile;
  selectFile() async {
    final file = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['png', 'jpg', 'jpeg']
    );
    if (file != null) {
      setState(() {
        _file = File(file.files.single.path!);
        _platformFile = file.files.first;
      });
    }

    loadingController.forward();
  }

  @override
  void initState() {
    loadingController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 10),
    )..addListener(() { setState(() {}); });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<InputFieldBloc<PlatformFile?, Object>,
        InputFieldBlocState<PlatformFile?, Object>>(
        bloc: fileFieldBloc,
        builder: (context, state) {
          return Scaffold(

i need to initialize final InputFieldBloc<PlatformFile?,Object> fileFieldBloc; so i could call it inside HomePage and _HomePageState

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

remove this line from your state

late final InputFieldBloc<PlatformFile?, Object> fileFieldBloc;

and use

bloc: widget.fileFieldBloc,
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading