尝试通过导航器页面访问时找不到Flutter scoped_model

时间:2018-12-23 10:07:20

标签: dart flutter

我正在使用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 个答案:

答案 0 :(得分:0)

您正在做的是尝试查找该上下文中没有的PageModel,因为您尚未为该特定小部件上下文创建任何内容。

您想将RaisedButton包裹在ScopedModelDescendant<PageModel>中并通过使用model.updateTitle()来更新模型。 这将在树中寻找最接近的PageModel祖先。