FilledStacks / flutter-tutorials

The repo contains the source code for all the tutorials on the FilledStacks Youtube channel.
MIT License
4.75k stars 1.76k forks source link

Exception on compile time with Flutter 2 #121

Closed alr2413 closed 3 years ago

alr2413 commented 3 years ago

@FilledStacks

In tutorial 31 (responsive architecture ...) the following error occurs on compile time, how can i solve it? thanks.

BaseModelWidget get widget => super.widget; A value of type 'Widget' can't be returned from the function 'widget' because it has a return type of 'BaseModelWidget'.

abstract class BaseModelWidget<T> extends Widget {
  @protected
  Widget build(BuildContext context, T model);

  @override
  DataProviderElement<T> createElement() => DataProviderElement<T>(this);
}

//
class DataProviderElement<T> extends ComponentElement {
  DataProviderElement(BaseModelWidget widget) : super(widget);

  @override
  BaseModelWidget get widget => super.widget;

  @override
  Widget build() => widget.build(this, Provider.of<T>(this));

  @override
  void update(BaseModelWidget newWidget) {
    super.update(newWidget);
    assert(widget == newWidget);
    rebuild();
  }
}
alr2413 commented 3 years ago

fix it by casting it to the BaseModelWidget.

super.widget as BaseModelWidget;