不支持的运算:Infinity 或 NaN toInt

问题描述 投票:0回答:4

从另一个页面启动幻灯片拼图项目时出错。

class PuzzlePage extends StatefulWidget {
    final int rows, columns;

    PuzzlePage({int columns = 3, int rows = 3})
    : columns = columns ?? 3,
    rows = rows ?? 3;

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

    class _PuzzlePageState extends State<PuzzlePage> with 
    WidgetsBindingObserver {

    BuildContext _context;

    @override
    void initState() {
    super.initState();

    }

    @override
    void didChangeDependencies(){
    super.didChangeDependencies();
    // Additional code
    }

    @override
    void deactivate() {
    super.deactivate();
    //this method not called when user press android back button or quit
    print('deactivate');
    }

     @override
     void dispose() {
     super.dispose();
     WidgetsBinding.instance.removeObserver(this);
     //this method not called when user press android back button or quit
     print('dispose');
     }

      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
      //print inactive and paused when quit
      print(state);
      print("lets do something here");
      }

      @override
      void didUpdateWidget(Widget oldWidget) {
      super.didUpdateWidget(oldWidget);
      print('didUpdateWidget');
      AppBuilder.of(_context).rebuild();

      }

      Widget build(BuildContext context) {
      return AppBuilder(
      builder: (context) {
      return _PuzzleHome(widget.rows, widget.columns);
      });
      }
      }

      class _PuzzleHome extends StatefulWidget {
      final int _rows, _columns;

      const _PuzzleHome(this._rows, this._columns, {Key key}) : super(key: 
      key);

      @override
      PuzzleHomeState createState() =>
          PuzzleHomeState(PuzzleAnimator(_columns, _rows));
      }


     // App Builder
     class AppBuilder extends StatefulWidget {
     const AppBuilder({Key key, this.builder}) : super(key: key);
     final Function(BuildContext) builder;

      @override
      AppBuilderState createState() => new AppBuilderState();

      static AppBuilderState of(BuildContext context) {
      return context.ancestorStateOfType(const TypeMatcher<AppBuilderState>());
      }
      }

      class AppBuilderState extends State<AppBuilder> {


      @override
      Widget build(BuildContext context) {
      return Container(
      child:widget.builder(context) ,
      );

      }


     void rebuild() {
     setState(() {});
     print('rebuild called');
     }
     }
flutter: #0      double.toInt (dart:core-patch/double.dart:183:36)
  flutter: #1      _BoxEdge.compareTo (package:flutter/src/semantics/semantics.dart:2194:41)
  flutter: #2      Comparable.compare (dart:core/comparable.dart:93:55)
  flutter: #3      ListMixin._compareAny (dart:collection/list.dart:340:23)
  flutter: #4      Sort._dualPivotQuicksort (dart:_internal/sort.dart:238:34)
  flutter: #5      Sort._doSort (dart:_internal/sort.dart:60:7)
  flutter: #6      Sort.sort (dart:_internal/sort.dart:33:5)
  flutter: #7      _ListBase&Object&ListMixin.sort (dart:collection/list.dart:334:10)
  flutter: #8      _childrenInDefaultOrder (package:flutter/src/semantics/semantics.dart:2391:9)
  flutter: #9      SemanticsNode._childrenInTraversalOrder (package:flutter/src/semantics/semantics.dart:1979:32)
  flutter: #10     SemanticsNode._addToUpdate (package:flutter/src/semantics/semantics.dart:1919:50)
  flutter: #11     SemanticsOwner.sendSemanticsUpdate (package:flutter/src/semantics/semantics.dart:2523:14)
  flutter: #12     PipelineOwner.flushSemantics (package:flutter/src/rendering/object.dart:961:23)
  flutter: #13     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:351:19)
  flutter: #14     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:13)
  flutter: #15     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5)
  flutter: #16     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1012:15)
  flutter: #17     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:952:9)
  flutter: #18     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:864:5)
  flutter: #22     _invoke (dart:ui/hooks.dart:219:10)
  flutter: #23     _drawFrame (dart:ui/hooks.dart:178:3)
  flutter: (elided 3 frames from package dart:async)
  flutter: 
  flutter: Another exception was thrown: NoSuchMethodError: The getter '_duration' was called on null. ```


flutter dart
4个回答
7
投票

当一个变量依赖于另一个变量时,就会出现此错误。

例如,对于变量 rows 和 columns,至少一个或另一个必须具有大于 0 的值。如果两者都为零或一个为 null,则会发生错误。

请注意,将 null 值传递给函数参数将覆盖默认值,并且 null 将优先。

int check({int value = 3}) {
 return value;
}

print(check(value: null)); // this will print null

5
投票

您可以检查值是无穷大还是南

if (this.isNaN || this.isInfinite){
   // default value
}else{
   // your logic
}

0
投票
    **Unsupported operation: Infinity or NaN toInt** occurs when the operation you try to perform is null or empty at the beginning try to avoid that!
    like by performing some check ups
    {
    // example
    if (projectList.isEmpty) {
         (projectList.length / 1)
    // something like this 
      }else{
    // something goes here
    }
    
    }

// **This answer is depending on my case!**

0
投票

实际上我也遇到了同样的错误。就我而言,我最初为变量设置了一些初始值,但在移动到另一个屏幕后,其工作是在前一个屏幕中进行一些更改...从新屏幕弹出后收到的新对象无论如何都会生成这些变量null...这是这个问题的主要原因。因此,在返回或弹出后,我确保再次设置这些变量值。

© www.soinside.com 2019 - 2024. All rights reserved.