2021年4月30日星期五

Return same object type user provided in Dart

So i have this Widget I am writing for flutter, it is a Tinder like swiping cards, I want the consumer to be able to provide a List of any type he wants, and i want to use the same type he provides to return in the builder method he should provide:

class Swipeable extends StatelessWidget {    final List<T> data;    final Widget Function(BuildContext, T) builder;      Swipeable({required this.data, required this.builder});  }  

Where T is the type of data the user provides, and it is controlled by the user with no limitations by me.

The consumer should be able to consume the widget like this:

Swipeable(    data: <User>[      User(        name: "Zakaria",        profession: "Geek",        images: [          "https://images.unsplash.com/photo-1533488069324-f9265c15d37f?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=594&q=80",          "https://images.unsplash.com/photo-1583864697784-a0efc8379f70?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80",        ],        age: 18,      )    ],    builder: (context, user) {      return Text(user.name);    }  )  

I hope u understood my Question, I'm not that good in explaining stuff when I'm still a newbie.

https://stackoverflow.com/questions/67341289/return-same-object-type-user-provided-in-dart May 01, 2021 at 06:49AM

没有评论:

发表评论