【问题标题】:type 'CategoriesScreen' is not a subtype of type 'String' in type cast类型“CategoriesScreen”不是类型转换中“String”类型的子类型
【发布时间】:2021-06-22 10:38:57
【问题描述】:

显示错误类型“CategoriesScreen”的飞镖不是类型转换中“字符串”类型的子类型。而 CategoryScreen 是另一个分配给标签栏屏幕的页面。而我尝试制作动态而不是 Object 但它不起作用。请给我解释一下

import 'package:dishes_app/Screens/Categories_screen.dart';
import 'package:dishes_app/Screens/Favourites_screen.dart';
import 'package:flutter/material.dart';

class TabsScreen extends StatefulWidget {
  @override
  _TabsScreenState createState() => _TabsScreenState();
}

class _TabsScreenState extends State<TabsScreen> {
  final List<Map<String, Object>> _pages = [
    {'page': CategoriesScreen(), 'title': 'Categories'},
    {'page': FavouriteScreen(), 'title': 'Your Favourite'}
  ];
  int selectedpageindex = 0;
  void _selectPage(int index) {
    setState(() {
      selectedpageindex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_pages[selectedpageindex]['page'] as String),
      ),
      body: _pages[selectedpageindex]['page'] as Widget,
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectPage,
        backgroundColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.white,
        selectedItemColor: Colors.amber,
        currentIndex: selectedpageindex,
        type: BottomNavigationBarType.shifting,
        items: [
          BottomNavigationBarItem(
              backgroundColor: Theme.of(context).primaryColor,
              icon: Icon(Icons.category),
              title: Text('Categories')),
          BottomNavigationBarItem(
            backgroundColor: Theme.of(context).primaryColor,
            icon: Icon(Icons.star),
            title: Text('the Favourite'),
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您收到此类型转换错误,因为您试图在下面的代码中将 widget 转换为 String

     appBar: AppBar(
       title: Text(_pages[selectedpageindex]['page'] as String),
      ),
    

    【讨论】:

    • 谢谢,它适用于字符串插值
    【解决方案2】:

    问题在于您的AppBar 您正试图将一个类放入字符串。

    用途:

    appBar: AppBar(
            title: Text(_pages[selectedpageindex]['title'] as String),
          ),
    

    代替:

    appBar: AppBar(
            title: Text(_pages[selectedpageindex]['page'] as String),
          ),
    

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多