简体   繁体   English

我在加载 CatDetailHeader 时遇到问题

[英]I am having trouble loadding CatDetailHeader

I hope all is well.我希望一切都好。 I have been following a video tutorial and I am having trouble with loading this second screen.我一直在关注视频教程,但在加载第二个屏幕时遇到了问题。 I am able to see the screen with the list of cats, but I can not load the detail page.我可以看到带有猫列表的屏幕,但无法加载详细信息页面。 I have copied the example code into my project and that did not work.我已将示例代码复制到我的项目中,但没有奏效。 I even tried loading the example project and that did not work either.我什至尝试加载示例项目,但也没有用。 I need a second set of eyes on this situation.我需要重新审视这种情况。

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════

The following assertion was thrown building CatDetailHeader(dirty, dependencies: [_LocalizationsScope-[GlobalKey#5f942], _InheritedTheme], state: _CatDetailHeaderState#2ac20):

'package:flutter/src/widgets/heroes.dart': Failed assertion: line 164 pos 15: 'tag != null': is not true.


The relevant error-causing widget was:

  CatDetailHeader file:///C:/Users/13363/AndroidStudioProjects/catbox/lib/ui/cat_details/details_page.dart:40:19

When the exception was thrown, this was the stack:

#2      new Hero (package:flutter/src/widgets/heroes.dart:164:15)

#3      _CatDetailHeaderState.build (package:catbox/ui/cat_details/header/details_header.dart:24:22)

#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)

#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)

#6      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)

...

════════════════════════════════════════════════════════════════════════════════════════════════
//Code from details_page.dart
import 'package:catbox/ui/cat_details/header/details_header.dart';
import 'package:catbox/models/cat.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

class CatDetailsPage extends StatefulWidget {
  final Cat cat;
  final Object avatarTag;

  CatDetailsPage(
      this.cat, {
        @required this.avatarTag,
      });

  @override
  _CatDetailsPageState createState() => new _CatDetailsPageState();
}

class _CatDetailsPageState extends State<CatDetailsPage> {
  @override
  Widget build(BuildContext context) {
    var linearGradient = new BoxDecoration(
      gradient: new LinearGradient(
        begin: FractionalOffset.centerRight,
        end: FractionalOffset.bottomLeft,
        colors: [
          Colors.blue,
          Colors.blue,
        ],
      ),
    );

    return new Scaffold(
      body: new SingleChildScrollView(
        child: new Container(
          decoration: linearGradient,
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children:[
              new CatDetailHeader(
                widget.cat,
                avatarTag: widget.avatarTag,
              ),
              //TODO Details Body
              //TODO Cat Showcase
            ],
          ),
        ),
      ),
    );
  }
}
//Code from detail_header.dart

import 'package:catbox/models/cat.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

class CatDetailHeader extends StatefulWidget {
  final Cat cat;
  final Object avatarTag;

  CatDetailHeader(
      this.cat, {
        @required this.avatarTag,
      });

  @override
  _CatDetailHeaderState createState() => new _CatDetailHeaderState();
}

class _CatDetailHeaderState extends State<CatDetailHeader> {
  @override
  Widget build(BuildContext context) {
    var theme = Theme.of(context);
    var textTheme = theme.textTheme;

    var avatar = new Hero(
      tag: widget.avatarTag,
      child: new CircleAvatar(
        backgroundImage: new NetworkImage(widget.cat.avatarUrl),
        radius: 75.0,
      ),
    );

    var likeInfo = new Padding(
      padding: const EdgeInsets.only(top: 16.0),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          new Icon(
            Icons.thumb_up,
            color: Colors.white,
            size: 16.0,
          ),
          new Padding(
              padding: const EdgeInsets.only(left: 8.0),
              child: new Text(
                widget.cat.likeCounter.toString(),
                style: textTheme.subhead.copyWith(color: Colors.white),
              )
          )
        ],
      ),
    );

    var actionButtons = new Padding(
      padding: const EdgeInsets.only(
        top: 16.0,
        left: 16.0,
        right: 16.0,
      ),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          new ClipRRect(
            borderRadius: new BorderRadius.circular(30.0),
            child: new MaterialButton(
              minWidth: 140.0,
              color: theme.accentColor,
              textColor: Colors.white,
              onPressed: () async {
                //TODO Handle Adopt
              },
              child: new Text('ADOPT ME'),
            ),
          ),
          new ClipRRect(
            borderRadius: new BorderRadius.circular(30.0),
            child: new RaisedButton(
              color: Colors.lightGreen,
              disabledColor: Colors.grey,
              textColor: Colors.white,
              onPressed: () async {
                //TODO Handle Like
              },
              child: new Text('LIKE'),
            ),
          ),
        ],
      ),
    );

    return new Stack(
      children: [
        //TODO Background Image
        new Align(
          alignment: FractionalOffset.bottomCenter,
          heightFactor: 1.4,
          child: new Column(
            children: [
              avatar,
              likeInfo,
              actionButtons,
            ],
          ),
        ),
        new Positioned(
          top: 26.0,
          left: 4.0,
          child: new BackButton(color: Colors.white),
        ),
      ],
    );
  }
}


You can copy paste run full code below您可以在下面复制粘贴运行完整代码
The error message means Hero tag is incorrect错误信息表示英雄标签不正确
In your code, you need to provide a value to avatarTag when call CatDetailsPage在您的代码中,您需要在调用CatDetailsPage时为avatarTag提供一个值
In ListView you can use index as part of tag name like below so it is uniqueListView您可以使用index作为标签名称的一部分,如下所示,因此它是唯一的

CatDetailsPage(cat, avatarTag: "tag${index}")

code snippet of demo code演示代码的代码片段

Expanded(flex: 1,child: CatDetailsPage(cat, avatarTag: "tag")),

working demo工作演示

在此处输入图片说明

full code完整代码

    import 'package:flutter/material.dart';

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

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }

    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);

      final String title;

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

    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      Cat cat = CatImplementation();
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Expanded(flex: 1,child: CatDetailsPage(cat, avatarTag: "tag")),
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.display1,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }

    class CatDetailsPage extends StatefulWidget {
      final Cat cat;
      final Object avatarTag;

      CatDetailsPage(
        this.cat, {
        @required this.avatarTag,
      });

      @override
      _CatDetailsPageState createState() => new _CatDetailsPageState();
    }

    class _CatDetailsPageState extends State<CatDetailsPage> {
      @override
      Widget build(BuildContext context) {
        var linearGradient = new BoxDecoration(
          gradient: new LinearGradient(
            begin: FractionalOffset.centerRight,
            end: FractionalOffset.bottomLeft,
            colors: [
              Colors.blue,
              Colors.blue,
            ],
          ),
        );

        return new Scaffold(
          body: new SingleChildScrollView(
            child: new Container(
              decoration: linearGradient,
              child: new Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  new CatDetailHeader(
                    widget.cat,
                    avatarTag: widget.avatarTag,
                  ),
                  //TODO Details Body
                  //TODO Cat Showcase
                ],
              ),
            ),
          ),
        );
      }
    }

    class CatDetailHeader extends StatefulWidget {
      final Cat cat;
      final Object avatarTag;

      CatDetailHeader(
        this.cat, {
        @required this.avatarTag,
      });

      @override
      _CatDetailHeaderState createState() => new _CatDetailHeaderState();
    }

    class _CatDetailHeaderState extends State<CatDetailHeader> {
      @override
      Widget build(BuildContext context) {
        var theme = Theme.of(context);
        var textTheme = theme.textTheme;

        var avatar = new Hero(
          tag: widget.avatarTag,
          child: new CircleAvatar(
            backgroundImage: new NetworkImage(widget.cat.avatarUrl),
            radius: 75.0,
          ),
        );

        var likeInfo = new Padding(
          padding: const EdgeInsets.only(top: 16.0),
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              new Icon(
                Icons.thumb_up,
                color: Colors.white,
                size: 16.0,
              ),
              new Padding(
                  padding: const EdgeInsets.only(left: 8.0),
                  child: new Text(
                    widget.cat.likeCounter.toString(),
                    style: textTheme.subhead.copyWith(color: Colors.white),
                  ))
            ],
          ),
        );

        var actionButtons = new Padding(
          padding: const EdgeInsets.only(
            top: 16.0,
            left: 16.0,
            right: 16.0,
          ),
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              new ClipRRect(
                borderRadius: new BorderRadius.circular(30.0),
                child: new MaterialButton(
                  minWidth: 140.0,
                  color: theme.accentColor,
                  textColor: Colors.white,
                  onPressed: () async {
                    //TODO Handle Adopt
                  },
                  child: new Text('ADOPT ME'),
                ),
              ),
              new ClipRRect(
                borderRadius: new BorderRadius.circular(30.0),
                child: new RaisedButton(
                  color: Colors.lightGreen,
                  disabledColor: Colors.grey,
                  textColor: Colors.white,
                  onPressed: () async {
                    //TODO Handle Like
                  },
                  child: new Text('LIKE'),
                ),
              ),
            ],
          ),
        );

        return new Stack(
          children: [
            //TODO Background Image
            new Align(
              alignment: FractionalOffset.bottomCenter,
              heightFactor: 1.4,
              child: new Column(
                children: [
                  avatar,
                  likeInfo,
                  actionButtons,
                ],
              ),
            ),
            new Positioned(
              top: 26.0,
              left: 4.0,
              child: new BackButton(color: Colors.white),
            ),
          ],
        );
      }
    }

    abstract class Cat {
      String get avatarUrl;
      int get likeCounter;
    }

    class CatImplementation extends Cat {
      String avatarUrl = "https://i.pravatar.cc/150?img=1";
      int likeCounter = 0;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在渲染容器和扩展面板列表 flutter 时遇到问题 - I am having trouble rendering a container and an expanded panel list flutter 我在遍历对象数组时遇到问题 - I am having trouble iterating through an array of objects 我在为我的 Flutter 应用程序创建一个简单的主页时遇到问题“未实现的静态目标丢失处理” - I am having trouble Creating A simple Hompage For my Flutter app "Unimplemented handling of missing static target" 我无法从 Google Place API 自动完成获取数据 - I am having trouble getting data from Google Place API AutoComplete 我当时无法使用.map语法理解Iterable类。 你能用一种简单的语言来表达吗? - I am having trouble understanding the Iterable class then with the .map syntax. can you put this in a simple language? 我在使用 Bloc/cubit 在 flutter 的其他小部件/类中使用 BlocBuilder 获取上次发出的 state 时遇到问题 - I am having trouble getting last emitted state with BlocBuilder in other widgets/classes in flutter using Bloc/cubit 我遇到了参数类型“void Function(bool)”的问题无法分配给参数类型“void Function(bool?)?” - I am having trouble with argument type 'void Function(bool)' can't be assigned to the parameter type 'void Function(bool?)?' 我在 Linux 上运行 Flutter 时遇到问题 - I'm having trouble running Flutter on Linux 我有一个相机 flutter 项目,我在制作自定义预览相机时遇到了麻烦,这里有没有人可以帮助我应该使用什么 package? - I have a camera flutter project, I am having trouble making a custom preview camera, is there anyone here to help me what package should I use? 我在绘制进度指示器时遇到问题 - I'm having trouble drawing the progress indicator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM