首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型'(String) => dynamic‘不是类型'Widget’的子类型

类型'(String) => dynamic‘不是类型'Widget’的子类型
EN

Stack Overflow用户
提问于 2020-08-17 18:27:03
回答 1查看 509关注 0票数 0

我正在尝试使用命名路由导航到一个页面。

主文件中的路由:

代码语言:javascript
复制
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FlutterShare',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.deepPurple,
        accentColor: Colors.teal,
      ),
      home: Home(),
      routes: {
        '/search': (BuildContext context) => Search(),
      },
    );
  }
}

推送到页面:

代码语言:javascript
复制
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('BookClub'),
        actions: [
          IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                Navigator.of(context).pushNamed('/search');
              })
        ],
      ),
      drawer: AppDrawer(),
    );
  }
}

错误:

代码语言:javascript
复制
type '(String) => dynamic' is not a subtype of type 'Widget'
The relevant error-causing widget was
  Search                            lib\main.dart:21

这是我第一次构建一个应用程序,我没有得到一个解决方案。main.dart文件中的路由中存在错误。

搜索代码:

代码语言:javascript
复制
class Search extends StatefulWidget {
  @override
  _SearchState createState() => _SearchState();
}

class _SearchState extends State<Search> {
  TextEditingController searchController = TextEditingController();
  Future<QuerySnapshot> searchResultsFuture;

      
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor.withOpacity(0.8),
      appBar: buildSearchField(),
      body: buildNoContent()
    );
  }
}

  AppBar buildSearchField() {
    return AppBar(
      backgroundColor: Colors.white,
      title: TextFormField(
        controller: searchController,
        decoration: InputDecoration(
          hintText: "Search for a book",
          filled: true,
          prefixIcon: Icon(
            Icons.account_box,
            size: 28.0,
          ),
          suffixIcon: IconButton(
            icon: Icon(Icons.clear),
            onPressed: null,
          ),
        ),
        onFieldSubmitted: null,
      ),
    );
  }

  Container buildNoContent() {
    final Orientation orientation = MediaQuery.of(context).orientation;
    return Container(
      child: Center(
        child: ListView(
          shrinkWrap: true,
          children: <Widget>[
            SvgPicture.asset(
              'assets/images/search.svg',
              height: orientation == Orientation.portrait ? 300.0 : 200.0,
            ),
            Text(
              "Find Users",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Colors.white,
                fontStyle: FontStyle.italic,
                fontWeight: FontWeight.w600,
                fontSize: 60.0,
              ),
            ),
          ],
        ),
      ),
    );
  }

希望这能有所帮助。在过去的3个小时里,我被困在这个问题上,找不到任何东西来解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-17 19:05:47

main.dart。您必须将起始页添加到initialRoute。您可以删除'home‘参数。

代码语言:javascript
复制
    initialRoute: HomeScreen(),
    routes: {
              "/search" (context) => Search()
          },

因此,将home.dart文件放在;

代码语言:javascript
复制
Navigator.of(context).pushNamed('/search');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63448996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档