Flutter动画不适用于提供者的数据

时间:2020-01-18 09:25:36

标签: flutter flutter-animation flutter-provider

我制作了一个渐变进度指示器,该指示器在我想添加动画之前一直工作良好。我的应用程序使用提供程序将所有内容存储在ChangeNotifier类中。我试图添加一个侦听器以动画化更改。当数据更改时,我得到:

════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following NoSuchMethodError was thrown while dispatching notifications for Tasks:
The getter 'owner' was called on null.
Receiver: null
Tried calling: owner

When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      Provider.of (package:provider/src/provider.dart:213:15)
#2      _GradientProgressIndicatorState.didChangeDependencies.<anonymous closure> (package:todo/components/gradient_progress_indicator.dart:50:29)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:206:21)
#4      Category.createTask (package:todo/models/category.dart:35:8)
...
The Tasks sending notification was: Instance of 'Tasks'
════════════════════════════════════════════════════════════════════════════════════════════════════

这是我的源代码:

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

import '../models/tasks.dart';

class GradientProgressIndicator extends StatefulWidget {
  final Color inactiveColor;
  final Gradient gradient;

  GradientProgressIndicator({this.inactiveColor, this.gradient});

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

class _GradientProgressIndicatorState extends State<GradientProgressIndicator>
    with SingleTickerProviderStateMixin {
  double oldValue = 0;
  AnimationController controller;
  Animation animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 500),
    );
    animation = Tween(
            begin: oldValue,
            end: Provider.of<Tasks>(context, listen: false)
                .currentCategory
                .taskPercentage)
        .animate(controller);
    oldValue = Provider.of<Tasks>(context, listen: false)
        .currentCategory
        .taskPercentage;
    controller.forward();
    controller.addListener(() {
      setState(() {});
    });
  }

  @override
  void didChangeDependencies() {
    Provider.of<Tasks>(context).addListener(() {
      animation = Tween(
              begin: oldValue,
              end: Provider.of<Tasks>(context, listen: false) //this is the line which the error mention
                  .currentCategory
                  .taskPercentage)
          .animate(controller);
      oldValue = Provider.of<Tasks>(context, listen: false)
          .currentCategory
          .taskPercentage;
      controller.forward();
    });
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(
          Radius.circular(5),
        ),
        color: widget.inactiveColor,
      ),
      height: 3,
      width: double.infinity,
      child: FractionallySizedBox(
        widthFactor: animation.value,
        alignment: Alignment.centerLeft,
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(5),
            ),
            gradient: widget.gradient,
          ),
        ),
      ),
    );
  }
}

Provider.of<Tasks>(context, listen: false).currentCategory.taskPercentage会返回一个介于0和1之间的双精度数)
这就是我从扑扑医生那里得到的:

[✓] Flutter (Channel master, v1.13.7-pre.12, on Linux, locale en_GB.UTF-8)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 3.5)
[✓] Connected device (1 available)

• No issues found!

有什么想法是问题的根源吗?

0 个答案:

没有答案