Home > database >  Is It Possible To Make One Parameter Can Accept Value/Dat from Different Function/Data Type?
Is It Possible To Make One Parameter Can Accept Value/Dat from Different Function/Data Type?

Time:01-27

I have two model dart. The one is PromoModel and the other one is DealsModel but the model has a similar structure (but not the same).

I only need one page (details page of promos and deals). So I thought, maybe it would be too redundant to have to create 2 detail pages, which are UI-like they are the same, and only differ from the parameters.

I see a lot of Functions or Classes that accept value/data with data type T. So is it possible to create only one class (UI/Page View) but can accept different Data types (in my case it is List PromoModel and DealsModel)?

CodePudding user response:

Yes, you can do that with dart generics

The page code will look something like this

class Page<T> extends StatelessWidget {
List<T> _pageData;
// Add other variable
// Add construtor
  @override
  Widget build(BuildContext context) {
    return Container(
     // Add stuff here
    );
  }
}

Then you can create the page like this

Page<DealsModel>();

But with this approach, depending with the structure of your model, you may need to write some conditional logic in the page to account the differences of the model structure.

  •  Tags:  
  • Related