Giter Club home page Giter Club logo

Comments (11)

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

This seems like an issue with your UI code rather than the library. Where are you using the clean architecture? Perhaps share some code.

from flutter_clean_architecture.

tusaamf avatar tusaamf commented on May 24, 2024

Sure, this is my code, the page use on TabBarView

class TheFirstPage extends View {
  final dynamic data;

  TheFirstPage({Key key, @required this.data}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _TheFirstPageView(data);
}

// error
class _TheFirstPageView extends ViewState<TheFirstPage, TheFirstController>
with AutomaticKeepAliveClientMixin<TheFirstPage> {
  _TheFirstPageView(dynamic data)
      : super(TheFirstController(SomeRepository(), data));

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    print('_TheFirstPageView initState');
  }

  @override
  Widget build(BuildContext context) {
    print('_TheFirstPageView build');
    super.build(context);
    return super.build(context);
  }

  @override
  Widget buildPage() {
    print('_TheFirstPageView buildPage');
    return Scaffold(
        body: Container(...)
    );
  }
}

// normally work
class _TheFirstPageState extends State<ChallengeDetailsPage>
    with AutomaticKeepAliveClientMixin<ChallengeDetailsPage> {

  @override
  void initState() {
    super.initState();
    print('_TheFirstPageState initState');
  }

  @override
  Widget build(BuildContext context) {
    print('_TheFirstPageState build');
    super.build(context);
    return Scaffold(
        body: Container()
    );
  }
  @override
  bool get wantKeepAlive => true;
}

from flutter_clean_architecture.

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

You're actually supposed to be using buildPage() instead of build() inside your ViewState as stated in the documentation. So just replace it and it should work. The library marks the build() function as nonvirtual, that means you should not be overriding it because it is used internally.

Do not override build() and use buildPage() for your UI such as

@override
  Widget buildPage() {
    print('_TheFirstPageState build');
    return Scaffold(
        body: Container()
    );

If you still have problem after this please let me know. Try this

class _TheFirstPageView extends ViewState<TheFirstPage, TheFirstController>
with AutomaticKeepAliveClientMixin<TheFirstPage> {
  _TheFirstPageView(dynamic data)
      : super(TheFirstController(SomeRepository(), data));

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    print('_TheFirstPageView initState');
  }

  @override
  Widget buildPage() {
    print('_TheFirstPageView buildPage');
    return Scaffold(
        body: Container()
    );
  }
}

from flutter_clean_architecture.

tusaamf avatar tusaamf commented on May 24, 2024

i try it but it show the same

image

i check on view.dart it actually wont call the super.build(context) like this sample https://github.com/diegoveloper/flutter-samples/blob/aa43501a8ee8799024a3422e39842329d20482c5/lib/persistent_tabbar/page2.dart

@override
  @nonVirtual
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Con>.value(
        value: _controller,
        child: Consumer<Con>(builder: (ctx, con, _) {
          _controller = con;
          return buildPage();
        }));
  }

from flutter_clean_architecture.

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

What happens if you add

@override
  Widget build(BuildContext context) {
    return super.build(context);
  }

Can you check if either build or buildPage gets called? If that doesn't help, I can try to recreate the issue in a small application and see what is causing the issue.

from flutter_clean_architecture.

tusaamf avatar tusaamf commented on May 24, 2024

I try to call super.build(context) on the build function of the Class that extends ViewState but it not work.

from flutter_clean_architecture.

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

Okay, I will make a small app and test this on my end.

from flutter_clean_architecture.

tusaamf avatar tusaamf commented on May 24, 2024

Any news, are you getting an error like me?

from flutter_clean_architecture.

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

I am looking into it as we speak. Will get back to you shortly.

from flutter_clean_architecture.

ShadyBoukhary avatar ShadyBoukhary commented on May 24, 2024

@tunt-04098,

I was able to replicate the issue on my end. I am reluctant to call this a bug, as this is an unfortunate side effect of how Dart works.

Mixins create interfaces on top of the base class. That means, if you were to extend a class with a method called foo() and mixin with a class that also has a method called foo(), only the Mixin's foo() method will be recognized, as the mixin becomes the proper super class of the child class.

Since both ViewState and AutomaticKeepAliveClientMixin override build(), only AutomaticKeepAliveClientMixin's build method is called, which means the library method won't be called and won't end up building your UI. That is the cause of the issue.

What that means for me, is that I will probably have to change the method name and replace it with a widget that users can use to wrap their widgets in their build method. Either that, or people would have to use the workaround below. While I eventually decide which route I am going to take, that is, either leave it for the user to work around it or modify the library in a less user-friendly way, you can go ahead and use the workaround below:

class TheFirstPage extends View {
  final dynamic data;

  TheFirstPage({Key key, @required this.data}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _TheFirstPageView(data);
}


class _TheFirstPageViewRename extends ViewState<TheFirstPage, TheFirstController> {
  _TheFirstPageViewRename(controller) : super(controller);
  Widget buildRename(context) => super.build(context);
  @override
  Widget buildPage() => null;
}
class _TheFirstPageView extends _TheFirstPageViewRename
with AutomaticKeepAliveClientMixin<TheFirstPage> {
  _TheFirstPageView(dynamic data)
      : super(TheFirstController(SomeRepository(), data));

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    print('_TheFirstPageView initState');
    
  }

  @override
  Widget build(BuildContext context) {
    print('_TheFirstPageView build');
    super.build(context);
    return super.buildRename(context);
  }

  @override
  Widget buildPage() {
    print('_TheFirstPageView buildPage');
    return Scaffold(
        body: Container()
    );
  }
}

This simply renames the method so Flutter can differentiate between the 2. Specifically here:

class _TheFirstPageViewRename extends ViewState<TheFirstPage, TheFirstController> {
  _TheFirstPageViewRename(controller) : super(controller);
  Widget buildRename(context) => super.build(context);
  @override
  Widget buildPage() => null;
}

It's only a few added lines of code, yet it is still sub-optimal.

Let me know if you have any questions.
Thank you and happy coding!

from flutter_clean_architecture.

tusaamf avatar tusaamf commented on May 24, 2024

Thank you for your help wish you many good things!

from flutter_clean_architecture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.