无法理解问题。按钮无法正常工作

时间:2019-03-22 20:10:48

标签: flutter

我遵循packpub中级课程的步骤,并且代码无法正常运行。我需要一些帮助来理解该异常。

在处理手势时引发了以下断言:

type 'List<dynamic>' is not a subtype of type 'List<PopupMenuEntry<Animals>>'

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => new _State();
  }

  enum Animals{Cat, Dog, Bird, Lizard, Fish}

class _State extends State<MyApp> {

  Animals _selected = Animals.Cat;
  String _value = 'Make a Selection';
  List _items = new List();

  @override
  void initState() {
    for(Animals animal in Animals.values) {
      _items.add(new PopupMenuItem(
          child: new Text(_getDisplay(animal),),
          value: animal,
      ));
    }
  }

  void _onSelected(Animals animal){
    setState((){
      _selected = animal;
      _value = 'You Selected ${_getDisplay(animal)}';
    });
  }

  String _getDisplay(Animals animal) {
    int index = animal.toString().indexOf('.');
    index++;
    return animal.toString().substring(index);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Name here'),
      ),
      body: new Container(
        padding: new EdgeInsets.all(32.0),
        child: new Center(
          child: new Row(
            children: <Widget>[
              new Container(
                padding: new EdgeInsets.all(5.0),
                child: new Text(_value),
              ),
              new PopupMenuButton<Animals>(
                  child: new Icon(Icons.input),
                  initialValue: Animals.Cat,
                  onSelected: _onSelected,
                  itemBuilder: (BuildContext context) {
                    return _items;
              }
              )
            ],
          ),
        ),
      ),
    );
  }
}

当我按下PopupMenuButton时,列表不会出现, 这就是我在控制台上得到的:

I/flutter (10194): ══╡ EXCEPTION CAUGHT BY GESTURE ╞══════════════════════════════════════════════════════════════════
I/flutter (10194): The following assertion was thrown while handling a gesture: I/flutter (10194): type 'List' is not a subtype of type 'List>'
I/flutter (10194): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (10194): more information in this error message to help you determine and fix the underlying cause.
I/flutter (10194): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (10194):
https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (10194): When the exception was thrown, this was the stack:
I/flutter (10194): #0 _State.build. (package:widget4/main.dart:67:21)
I/flutter (10194): #1 _PopupMenuButtonState.showButtonMenu (package:flutter/src/material/popup_menu.dart:900:21)
I/flutter (10194): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
I/flutter (10194): #3 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:568:30)
I/flutter (10194): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
I/flutter (10194): #5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
I/flutter (10194): #6 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
I/flutter (10194): #7 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:369:9)
I/flutter (10194): #8 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
I/flutter (10194): #9 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
I/flutter (10194): #10
_WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
I/flutter (10194): #11
_WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
I/flutter (10194): #12
_WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
I/flutter (10194): #13
_WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
I/flutter (10194): #14
_WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
I/flutter (10194): #18 _invoke1 (dart:ui/hooks.dart:223:10)
I/flutter (10194): #19 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
I/flutter (10194): (elided 3 frames from package dart:async)
I/flutter (10194): Handler: onTap
I/flutter (10194): Recognizer:
I/flutter (10194): TapGestureRecognizer#7540f(debugOwner: GestureDetector, state: accepted, won arena, finalPosition:
I/flutter (10194): Offset(156.5, 334.8), sent tap down) ══════════════════════════════════════════════════════════════
I/flutter (10194): Another exception was thrown: type 'List' is not a subtype of type 'List>'

0 个答案:

没有答案