尝试使用导航器页面访问时找不到 Flutter scoped_model

Posted

技术标签:

【中文标题】尝试使用导航器页面访问时找不到 Flutter scoped_model【英文标题】:Flutter scoped_model not find when try to access with Navigator page 【发布时间】:2018-12-23 10:07:19 【问题描述】:

我正在使用flutter scoped_model,当我们尝试从子Widget访问ScopedModel时,它能够无误地访问。

但是当我尝试从使用 Navigator.push 加载的 Widget 访问它时,相同的代码无法使用,它给出错误错误:找不到正确的 ScopedModel。

PageModel 在首页声明。

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class PageModel extends Model 
    String title;

    static PageModel of(BuildContext context) =>
        ScopedModel.of<PageModel>(context);

    loadTitle() 
      title = 'Old Title ';
    

    updateTitle() 
      title = 'New Title';
      notifyListeners();
    


class HomePage extends StatefulWidget 
  @override
  _HomePageState createState() => _HomePageState();


class _HomePageState extends State<HomePage> 
  final PageModel model = PageModel();

  @override
  Widget build(BuildContext context) 
    return ScopedModel<PageModel>(
      model: model,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Home'),
        ),
        body: HomePageBody(),
      ),
    );
  


class HomePageBody extends StatefulWidget 
  @override
  _HomePageBodyState createState() => _HomePageBodyState();


class _HomePageBodyState extends State<HomePageBody> 
  @override
  void initState() 
    PageModel.of(context).loadTitle();
    super.initState();
  

  @override
  Widget build(BuildContext context) 
    return ScopedModelDescendant<PageModel>(
        builder: (BuildContext context, child, PageModel model) 
      return Column(
        children: <Widget>[
          Text(model.title),
          RaisedButton(
            child: Text('Edit'),
            onPressed: () 
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => DetailPage(), fullscreenDialog: true),
              );
            ,
          ),
        ],
      );
    );
  


class DetailPage extends StatefulWidget 
  @override
  _DetailPageState createState() => _DetailPageState();


class _DetailPageState extends State<DetailPage> 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text('Edit Page'),
      ),
      body: RaisedButton(
        child: Text('Update'),
        onPressed: () 
          PageModel.of(context).updateTitle();
        ,
      ),
    );
  

从 DetailPage 当我们调用 PageModel.of(context).updateTitle();出现以下错误,

/flutter (26710): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (26710): The following ScopedModelError was thrown while handling a gesture:
I/flutter (26710): Error: Could not find the correct ScopedModel.
I/flutter (26710):     
I/flutter (26710): To fix, please:
I/flutter (26710):           
I/flutter (26710):   * Provide types to ScopedModel<MyModel>
I/flutter (26710):   * Provide types to ScopedModelDescendant<MyModel> 
I/flutter (26710):   * Provide types to ScopedModel.of<MyModel>() 
I/flutter (26710):   * Always use package imports. Ex: `import 'package:my_app/my_model.dart';
I/flutter (26710):   
I/flutter (26710): If none of these solutions work, please file a bug at:
I/flutter (26710): https://github.com/brianegan/scoped_model/issues/new

【问题讨论】:

【参考方案1】:

你正在做的是试图找到一个没有的上下文的PageModel,因为你还没有为那个特定的小部件上下文创建任何上下文。

您想将RaisedButton 包装在ScopedModelDescendant&lt;PageModel&gt; 中并改用model.updateTitle() 更新您的模型。 这将在树中寻找最近的 PageModel 祖先。

【讨论】:

以上是关于尝试使用导航器页面访问时找不到 Flutter scoped_model的主要内容,如果未能解决你的问题,请参考以下文章

将 Google 地图添加到 Flutter 时找不到 API 密钥

IE导航到本地文件时找不到基于HWND的iexplorer进程?

使用命名路由导航时找不到流提供程序

iOS XCode - 为Profile构建时找不到Swift类

通过 VSCode 运行 Flutter 项目时找不到名为“web-server-debug-protocol”的选项

如果在 WebView Flutter 中导航失败,则重新加载页面