【问题标题】:Flutter scoped_model not find when try to access with Navigator page尝试使用导航器页面访问时找不到 Flutter scoped_model
【发布时间】: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

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多