【问题标题】:Rive Flutter LateInitializationError: Field 'practiceArtboard' has not been initializedRive Flutter LateInitializationError:字段'practiceArtboard'尚未初始化
【发布时间】:2022-07-12 21:50:14
【问题描述】:

我导入了一个具有状态机的 rive 文件,我正在我的应用程序的 initState 中初始化画板,但我仍然收到延迟初始化错误。如果有人帮助我,我将不胜感激!以下是您可能需要的一些必要信息:

Rive 文件名:practice.riv

状态机名称:动画

布尔输入名称:boolInput

Here is a picture of how the state machine looks in rive

这是我编写的在 Flutter 中使用 Rive 文件的代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rive/rive.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late SMIInput<bool>? input;
  late Artboard? practiceArtboard;

  @override
  void initState() {
    super.initState();
    rootBundle.load('assets/rive/practice.riv').then((data) {
      final file = RiveFile.import(data);
      final artboard = file.mainArtboard;
      var controller = StateMachineController.fromArtboard(artboard, 'animations');
      if(controller != null) {
        artboard.addController(controller);
        input = controller.findInput('boolInput');
      }
      setState(() {
        practiceArtboard = artboard;
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: practiceArtboard == null ? Container() : Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(height:200, width: 200 ,child: Rive(artboard: practiceArtboard!,)),
            SizedBox(height: 10,),
            ElevatedButton(onPressed: () {
              input?.value = true;
            }, child: Text('firstAnimation')),
            SizedBox(height: 10,),
            ElevatedButton(onPressed: () {
              input?.value = false;
            }, child: Text('secondAnimation')),
          ],
        ),
      ),
    );
  }
}

我欢迎任何提示和建议,因为我不是很有经验。谢谢。

【问题讨论】:

    标签: flutter rive


    【解决方案1】:

    你正在使用一种过时的方式

    请参阅 RIVE 指南: https://help.rive.app/runtimes/state-machines

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 2021-12-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 2021-10-27
      • 2021-12-29
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多