我正在从Udemy教程中制作餐厅应用,它显示给我这个错误:

huangapple go评论54阅读模式
英文:

I making Restaurant app form Udemy tutorial it show me this error

问题

我面临这个错误

错误: 无法将参数类型'Object?'分配给参数类型'Widget?'tabs_screen.dart:34
  • Object 来自 dart:core
  • Widget 来自 package:flutter/src/widgets/framework.dart
('/C:/flutter_windows_3.3.4-stable/flutter/packages/flutter/lib/src/widgets/framework.dart')framework.dart:1

body: _pages[_selectPageIndex]['page']

代码

class TapScreen extends StatefulWidget {
  const TapScreen({super.key});

  @override
  State<TapScreen> createState() => _TapScreenState();
}

List<Map<String, Object>> _pages = [
  {'page': CategoriesScreen(), 'title': 'Catergories'},
  {'page': FavoitesScreen(), 'title': 'your Favorites'}
];

class _TapScreenState extends State<TapScreen> {
  int _selectPageIndex = 0;
  void _selectPage(int index) {
    setState(() {
      _selectPageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("meal"),
      ),
      body: _pages[_selectPageIndex]['page'],
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: _selectPageIndex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            label: 'Favorites',
          ),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.category),
            label: 'Favorites',
          ),
        ],
      ),
    );
  }
}
英文:

I facing this error

Error: The argument type &#39;Object?&#39; can&#39;t be assigned to the parameter type &#39;Widget?&#39;.
tabs_screen.dart:34
  • Object is from dart:core.
  • Widget is from package:flutter/src/widgets/framework.dart
(&#39;/C:/flutter_windows_3.3.4-stable/flutter/packages/flutter/lib/src/widgets/framework.dart&#39;).
framework.dart:1

body: _pages[_selectPageIndex][&#39;page&#39;].

Code

class TapScreen extends StatefulWidget {
  const TapScreen({super.key});

  @override
  State&lt;TapScreen&gt; createState() =&gt; _TapScreenState();
}

List&lt;Map&lt;String, Object&gt;&gt; _pages = [
  {&#39;page&#39;: CategoriesScreen(), &#39;title&#39;: &#39;Catergories&#39;},
  {&#39;page&#39;: FavoitesScreen(), &#39;title&#39;: &#39;your Favorites&#39;}
];

class _TapScreenState extends State&lt;TapScreen&gt; {
  int _selectPageIndex = 0;
  void _selectPage(int index) {
    setState(() {
      _selectPageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(&quot;meal&quot;),
      ),
      body: _pages[_selectPageIndex][&#39;page&#39;],
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: _selectPageIndex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            label: &#39;Favorites&#39;,
          ),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.category),
            label: &#39;Favorites&#39;,
          ),
        ],
      ),
    );
  }
}

答案1

得分: 2

在Dart中,Object可以是任何东西,而scaffold的主体仅期望Widget类型,因此会导致错误。最好始终使用Widget类型注释。

在您的情况下,检查该Object是否为widget类型或不是

_pages[_selectPageIndex]['page'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found");

完整代码

class TapScreen extends StatefulWidget {
  const TapScreen({super.key});

  @override
  State<TapScreen> createState() => _TapScreenState();
}

List<Map<String, Object>> _pages = [
  {'page': CategoriesScreen(), 'title': 'Catergories'},
  {'page': FavoitesScreen(), 'title': 'your Favorites'}
];

class _TapScreenState extends State<TapScreen> {
  int _selectPageIndex = 0;
  void _selectPage(int index) {
    setState(() {
      _selectPageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("meal"),
      ),
      body: _pages[_selectPageIndex]['page'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found"), // 在这里更改
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: _selectPageIndex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            label: 'Favorites',
          ),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.category),
            label: 'Favorites',
          ),
        ],
      ),
    );
  }
}
英文:

In Dart Object can be anything and the body of the scaffold expects only the type Widget thus causing the error. It is always better to use Widget type annotation.

In your case, Check if that Object is a widget type or not

 _pages[_selectPageIndex][&#39;page&#39;] is Widget ?  _pages[_selectPageIndex][&#39;page&#39;] as Widget : Text(&quot;Page not found&quot;);

Full code

class TapScreen extends StatefulWidget {
  const TapScreen({super.key});

  @override
  State&lt;TapScreen&gt; createState() =&gt; _TapScreenState();
}

List&lt;Map&lt;String, Object&gt;&gt; _pages = [
  {&#39;page&#39;: CategoriesScreen(), &#39;title&#39;: &#39;Catergories&#39;},
  {&#39;page&#39;: FavoitesScreen(), &#39;title&#39;: &#39;your Favorites&#39;}
];

class _TapScreenState extends State&lt;TapScreen&gt; {
  int _selectPageIndex = 0;
  void _selectPage(int index) {
    setState(() {
      _selectPageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(&quot;meal&quot;),
      ),
      body: _pages[_selectPageIndex][&#39;page&#39;] is Widget ? _pages[_selectPageIndex][&#39;page&#39;] as Widget : Text(&quot;Page not found&quot;), // Change here
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: _selectPageIndex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            label: &#39;Favorites&#39;,
          ),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.category),
            label: &#39;Favorites&#39;,
          ),
        ],
      ),
    );
  }
}

答案2

得分: 1

body: _pages[_selectPageIndex]['page'] as Widget? ?? Text("not found"),
英文:

You can provide default widget on null case.

body: _pages[_selectPageIndex][&#39;page&#39;] as Widget? ?? Text(&quot;not found&quot;),

huangapple
  • 本文由 发表于 2023年2月18日 21:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定