Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported operation: Infinity or NaN toInt

Tags:

flutter

dart

Error when launching a slide puzzle project from another page.

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. ```


like image 356
George John Avatar asked Jun 08 '19 23:06

George John


2 Answers

This error occurs when a variable is dependant on another variable.

For example, with variables rows and columns, at least one or another must-have a value greater than 0. if both are zero or one is null then the error occurs.

Please do note that passing null value to a function parameter will override the default value and the null will take precedence.

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

print(check(value: null)); // this will print null
like image 155
Alish Giri Avatar answered Oct 20 '22 03:10

Alish Giri


You can check value is infinity or nan

if (this.isNaN || this.isInfinite){
   // default value
}else{
   // your logic
}
like image 31
BIS Tech Avatar answered Oct 20 '22 03:10

BIS Tech