【问题标题】:The getter '_controller' was called on null. Flutter在 null 上调用了 getter '_controller'。扑
【发布时间】:2021-06-11 12:17:37
【问题描述】:

我在 Flutter 中使用这个包:https://pub.dev/packages/circular_countdown_timer

但是,我需要创建一个单独的小部件来稍微分离我的代码。我创造了这样的东西:

import 'package:circular_countdown_timer/circular_countdown_timer.dart';
import 'package:flutter/material.dart';

class CustomOtpCard extends StatefulWidget {
  final String duration;
  final String accountName;
  final String otp;
  final Function(CountDownController) onComplete;

  CustomOtpCard({
    @required this.duration,
    @required this.accountName,
    @required this.otp,
    @required this.onComplete,
  });

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

class _CustomOtpCardState extends State<CustomOtpCard> {
  CountDownController _controller = CountDownController();

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        leading: CircularCountDownTimer(
          duration: int.parse(widget.duration),
          onComplete: widget.onComplete(_controller),
          initialDuration: 0,
          ringColor: Colors.grey[300],
          fillColor: Colors.purple,
          backgroundColor: Colors.purple[500],
          width: 50.0,
          height: 50.0,
        ),
        title: Text(widget.accountName),
        subtitle: Text(widget.otp),
      ),
    );
  }
}

现在,我是这样使用它的:

return ListView.builder(
                itemCount: _totpItems.length,
                itemBuilder: (context, index) {
                  return CustomOtpCard(
                    duration: '30',
                    onComplete: (controller) {
                      BlocProvider.of<TotpGeneratorBloc>(context)
                          .add(GetTotpItemsEvent());

                      controller.restart();
                    },
                    accountName: _totpItems[index].accountName,
                    otp: _totpItems[index].otp,
                  );
                },
              );

然后错误显示“在 null 上调用了 getter '_controller'。”。这是我的错误的图像:

它说,我传递给回调的 _controller 是空的。我究竟做错了什么?谁能给我一个提示?

谢谢!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我假设当您调用controller.restart() 时,_CustomOtpCardState 已被处理,然后_controller 不再存在。

    你可以这样做:

    首先将onComplete 转为VoidCallback(相当于void Function()):

    final VoidCallback onComplete;
    

    那就这样用吧:

    ...
    duration: int.parse(widget.duration),
    onComplete: () {
        widget.onComplete();
        _controller.restart();
    },
    initialDuration: 0,
    ...
    

    在你的ListView.builder

    return CustomOtpCard(
       duration: '30',
       onComplete: () {
           BlocProvider.of<TotpGeneratorBloc>(context)
                .add(GetTotpItemsEvent());
       },
       accountName: _totpItems[index].accountName,
       otp: _totpItems[index].otp,
    );
    

    【讨论】:

    • 不,它不起作用。 onComplete 回调不会触发 BlocProvider。
    • 当我触发 BlocProvider 时,它显示了同样的错误:“getter '_controller' was called on null”。
    • 您的错误是否有任何堆栈跟踪?
    • 好吧,我找到了罪魁祸首。我忘记将控制器参数添加到 CircularCountDownTimer。
    猜你喜欢
    • 2021-03-06
    • 2021-01-19
    • 1970-01-01
    • 2021-12-21
    • 2020-01-10
    • 2020-10-24
    • 2021-04-13
    • 2021-10-22
    • 2020-05-20
    相关资源
    最近更新 更多