Home > OS >  How to call final outside class in flutter?
How to call final outside class in flutter?

Time:09-13

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

CodePudding user response:

remove this line from your state

late final InputFieldBloc<PlatformFile?, Object> fileFieldBloc;

and use

bloc: widget.fileFieldBloc,

CodePudding user response:

You can use widget.fileFieldBloc to call this field from the state class

  • Related