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);
}