Flutter / Dart错误:_MyAppState@16036154.build

时间:2018-07-09 17:29:27

标签: dart flutter

在尝试遵循此tutorial时遇到了以下错误。奇怪的是,Google搜索此错误消息并没有带来任何有用的见解。

错误消息:

I/flutter ( 5472): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 
╞═══════════════════════════════════════════════════════════
I/flutter ( 5472): The following _CompileTimeError was thrown building MyApp(dirty, state: _MyAppState#f8841):
I/flutter ( 5472): 'file:///Users/micah/MobileApps/flutter_test_1/lib/main.dart': error: Unexpected tag 0 (Nothing) in
I/flutter ( 5472): _MyAppState@16036154.build, expected a procedure, a constructor or a function node
I/flutter ( 5472):
I/flutter ( 5472): When the exception was thrown, this was the stack:
I/flutter ( 5472): #0      StatefulElement.build (package:flutter/src/widgets/framework.dart:3730:27)

main.dart文件:

import 'package:flutter/material.dart';

//    import './product_manager.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      //  appBar: AppBar(
      //    title: Text('EasyList'),
      //  ),
      //  body: ProductManager(),
      ),
    );
  }
}

products.dart文件:

import 'package:flutter/material.dart';

class Products extends StatelessWidget {
  final List<String> products;

  Products(this.products);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: products
        .map(
          (element) => Card(
                child: Column(
                  children: <Widget>[
                    Image.asset('assets/food.jpg'),
                    Text(element)
                  ],
                ),
              ),
        )
        .toList()
      );
  }
}

product_manager.dart文件:

import 'package:flutter/material.dart';

import './products.dart';

class ProductManager extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ProductManagerState();
  }
}

class _ProductManagerState extends State<ProductManager> {
  List<String> _products = ['Food Tester'];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.all(10.0),
          child: RaisedButton(
            onPressed: () {
              setState(() {
                _products.add('Advanced Food Tester');
              });
            },
            child: Text('Add Product'),
          ),
        ),
        Products(_products)
      ],
    );
  }
}

1 个答案:

答案 0 :(得分:5)

单击“重新启动调试”解决了该问题。

我认为问题可能与以下事实有关:我从StatefullWidget更改为StatelessWidget而没有重新启动调试器。