0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Draggable使用時に"Could not find the correct Provider"エラーが出た場合の対処法

Posted at

Draggableでドラッグ可能なWidgetを実装したところ、
ドラッグ時にProviderのエラーが発生しました。

Could not find the correct Provider
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building Consumer<HomeViewModel>(dirty):
Error: Could not find the correct Provider<HomeViewModel> above this Consumer<HomeViewModel> Widget

This happens because you used a `BuildContext` that does not include the provider

この時のコードは以下です。

Widget build(BuildContext context) {
  return Draggable(
    child: method(),
    feedback: method(),
  );
}
Widget method() {
  return Consumer<HomeViewModel>(builder: (context, model, child) {
    return Text(model.text);
  });
}

原因は feedback の中で Consumer を使用しているためです。
Draggable よりも前に Consumer を使えば解決します。

Widget build(BuildContext context) {
  return Consumer<HomeViewModel>(builder: (context, model, child) {
    return Draggable(
      child: method(model),
      feedback: method(model),
    );
  });
}
Widget method(HomeViewModel model) {
  return Text(model.text);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?