【问题标题】:How to add Dependency Injection in Flutter with Provider如何在 Flutter 中使用 Provider 添加依赖注入
【发布时间】:2020-07-19 22:01:12
【问题描述】:
void main() => runApp(MultiProvider(providers: [
      ChangeNotifierProvider(
        create: (BuildContext context) => QuoteViewModel(),
      ),
      ChangeNotifierProvider(
        create: (BuildContext context) => AuthorViewModel(),
      ),
    ], child: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Poems',
      home: PoemsListPage(),
    );
  }
}

main.dart

class QuoteListPage extends StatefulWidget {
  @override
  _QuoteListPageState createState() => _QuoteListPageState();
}

class _QuoteListPageState extends State<QuoteListPage> {
  @override
  Widget build(BuildContext context) {
    final model = Provider.of<QuoteViewModel>(context);
    return Scaffold(
      appBar: AppBar(title: Text('Poems List')),
      body: _buildBody(model.getQuotes()),
    );
  }

  Widget _buildBody(List<Quote> quotes) => ListView(
        children: quotes
            .map((quote) => ListTile(
                  title: Text(quote.body),
                ))
            .toList(),
      );
}

quoteListPage.dart

class Quote {
  final String id;
  final String authorId;
  final String body;

  Quote(this.id, this.authorId, this.body);
}

class Author {
  final String id;
  final String name;

  Author(this.id, this.name);
}

models.dart

class Repository {
  final Api _api = Api();
  final Database _database = Api();

  List<Quote> getQuotes() => [];

  List<Author> getAuthors() => [];
}

repository.dart

class QuoteViewModel with ChangeNotifier {
  final Repository _repository = Repository();

  List<Quote> getQuotes() => _repository.getQuotes();
}

class AuthorViewModel with ChangeNotifier {
  final Repository _repository = Repository();

  List<Author> getAuthors() => _repository.getAuthors();
}

notifiers.dart

我在阅读Medium 上的一篇文章后尝试使用 ProxyProvider,但它以错误“这可能是一个错误,因为提供者不会自动更新依赖项”结束

List<SingleChildWidget> globalProviders = [
  Provider.value(value: Api()),
  Provider.value(value: Database()),
  ProxyProvider2<Api, Database, Repository>(
    update: (_, api, database, __) => Repository(api, database),
  ),
  ProxyProvider<Repository, QuoteViewModel>(
    update: (_, repo, __) => QuoteViewModel(repo),
  ),
  ProxyProvider<Repository, AuthorViewModel>(
    update: (_, repo, __) => AuthorViewModel(repo),
  )
];

void main() => runApp(
      MultiProvider(
        providers: globalProviders,
        child: MyApp(),
      ),
    );

main.dart(更新)

class Repository {
  final Api _api;
  final Database _database;

  Repository({@required api, @required database})
      : _api = api,
        _database = database;

  List<Quote> getQuotes() => [];

  List<Author> getAuthors() => [];
}

repository.dart(更新)

class QuoteViewModel with ChangeNotifier {
  final Repository _repository;

  QuoteViewModel({@required repository}) : _repository = repository;

  List<Quote> getQuotes() => _repository.getQuotes();
}

class AuthorViewModel with ChangeNotifier {
  final Repository _repository;

  AuthorViewModel({@required repository}) : _repository = repository;

  List<Author> getAuthors() => _repository.getAuthors();
}

notifiers.dart(更新)

【问题讨论】:

  • 如果您可以只提供有问题的代码而不是项目中的所有代码,这将对其他人有所帮助。这样人们就可以专注于您的问题,而无需滚动数百行来查找错误代码
  • 找到解决方案了吗?我还在寻找如何通过 Multiprovider 为更改通知程序提供其依赖项的方法。
  • 这可能会有所帮助:stackoverflow.com/questions/57765994/…

标签: flutter dart dependency-injection architecture provider


【解决方案1】:

Flutter 有一个使用提供程序包的指南,可以帮助您。 https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple 如果您能更具体地说明您与提供商遇到的问题,将会很有帮助。

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 2021-11-25
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多