簡體   English   中英

如何在 flutter 中調用 class 之外的最終結果?

[英]How to call final outside class in flutter?

我的表單塊顯示警報“LateInitializationError:字段'fileFieldBloc'尚未初始化”。 我初始化final InputFieldBloc<PlatformFile?,Object> fileFieldBloc; class HomePage extends StatefulWidget這是一個有狀態的小部件,但我不能在class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin調用fileFieldBloc與 SingleTickerProviderStateMixin ,我有什么辦法初始化它嗎? 這是代碼:

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(

我需要初始化final InputFieldBloc<PlatformFile?,Object> fileFieldBloc; 所以我可以在HomePage_HomePageState中調用它

從您的 state 中刪除此行

late final InputFieldBloc<PlatformFile?, Object> fileFieldBloc;

並使用

bloc: widget.fileFieldBloc,

您可以使用widget.fileFieldBloc從 state class 調用此字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM