Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Flutter's mixin in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what is the use of Flutter mixin in Android. I hope you will get something after reading this article. Let's discuss it together.

What is mixin?

How should mixin be understood? for me, who is from the Java department, this is a new concept, and the introduction of all kinds of materials has not found a clear definition. From a personal point of view, it can be thought of as an interface in Kotlin (the difference from Java is that it can take non-abstract properties and methods), while multiple mixin can cover each other to achieve composition, providing a great deal of flexibility and similar to multiple inheritance.

Page table page

This is a normal display of data, pull up and load more data list.

There is a data list listData of type List, a page data for paging, isLoading to determine whether the data is being loaded, and scrollController for the list controller

If there are a large number of such pages, you can use mixin to process them, resulting in a lot of repetitive code.

Import 'package:flutter/material.dart';import' package:flutter_app/app/model/ListViewJson.dart';import 'package:flutter_app/app/shared/api/api.dart';import' package:dio/dio.dart';import 'dart:convert';import' package:flutter_app/app/shared/mixins/list_more_data_mixin.dart';/// list page class RecommendView extends StatefulWidget {@ override _ RecommendViewState createState () = > _ RecommendViewState () } class _ RecommendViewState extends ListMoreDataBase with ListMoreDataMixin {@ override Future getData () async {String data = await DioUtils.postHttp ("api/getOneLevel", parameters: FormData.fromMap ({'page': page,' limit':'10 percent,}),); ListViewJson _ json = ListViewJson.fromJson (json.decode (data)); return _ json.data;} @ override void initState () {print ('init widget'); super.initState ();} @ override void dispose () {print (' dispose widget') Super.dispose ();} @ override Widget build (BuildContext context) {return Scaffold (backgroundColor: Colors.white, appBar: AppBar (title: Text ('return')), body: Stack (children: [NotificationListener (onNotification: onNotification, child: ListView.builder (controller: scrollController, itemCount: listData.length, itemBuilder: (BuildContext context, int index) = > TeamListItem (list data [index]), isLoading? Center (child: CircularProgressIndicator ()): Container ()],),);}}

Mixin

Import 'package:flutter/material.dart';abstract class ListMoreDataBase extends State {/ / get asynchronous data Future getData ();} / / on mixin ListMoreDataMixin on ListMoreDataBase {@ override void initState () {print (' init'); super.initState (); initData ();} @ override void dispose () {print ('dispose'); super.dispose (); scrollController?.dispose ();} / data list List listData = []; / / paging int page = 1 / / whether data bool isLoading = false; / / scrollbar controller ScrollController scrollController = ScrollController (); / / initialize data Future initData () async {setState (() {isLoading = true;}); List data = await getData (); if (! mounted) return; setState (() {listData = data; isLoading = false;});} / / pull-up load more Future loadMore () async {setState (() {isLoading = true; page + = 1) }); List data = await getData (); if (data.isEmpty) {page--;} setState (() {listData.addAll (data); isLoading = false;});} bool canLoadMore (ScrollNotification scroll) {return! isLoading & & scroll.metrics.maxScrollExtent init-> dispose widget-> dispose

Ps: the following demonstrates the use of mixin in Dart, from simple to complex

The simplest mixin

Mixin TestMixin {void test () {print ('test');} int testInt = 1; void test2 ();} class Test with TestMixin {@ override test2 () {print (' test2');}} void main () {Test () .test (); / / test print (Test () .testInt); / / 1 Test () .test2 (); / / test2}

Mixin itself can be abstract, can define various method properties, can also be abstract, and other subsequent classes to implement

Based on a certain type of mixin

Class BaseObject {void method () {print ('call method');}} mixin TestMixin on BaseObject {void test () {print (' test');} int testInt = 1; void test2 () {method ();}} class Test extends BaseObject with TestMixin {} void main () {Test (). Test (); / / test print (Test () .testInt); / / 1 Test (). Test2 (); / / call method}

When the on keyword is used, it means that the mixin can only be used in a subclass of that class, so it is obvious that the methods and properties defined by that class can be called in mixin.

Multiple mixin

Mixin TestMixin {void test () {print ('test');} int testInt = 1; void test2 ();} mixin TestMixin2 {int testInt = 2; void test3 () {print (' test3');}} class Test with TestMixin, TestMixin2 {@ override test2 () {print ('test2');}} void main () {Test (). Test (); / / test print (Test () .testInt); / / 2 Test (). Test2 (); / / test2 Test (). Test3 () / / test3}

If you change the order of TestMixin and TestMixin2:

Mixin TestMixin {void test () {print ('test');} int testInt = 1; void test2 ();} mixin TestMixin2 {int testInt = 2; void test3 () {print (' test3');}} class Test with TestMixin2, TestMixin {@ override test2 () {print ('test2');}} void main () {Test (). Test (); / / test print (Test () .testInt); / / 1 Test (). Test2 (); / / test2 Test (). Test3 () / / test3}

If there is a conflicting part of the mixin, it will be overwritten later, and if there is no conflict, it will be retained, so there can be a situation in which the later mixin modifies part of the logic of the previous mixin, and overrides can be achieved without direct inheritance, avoiding more complex inheritance relationships.

"multiple inheritance" mixin TestMixin on BaseClass {void init () {print ('TestMixin init start'); super.init (); print (' TestMixin init end');}} mixin TestMixin2 on BaseClass {void init () {print ('TestMixin2 init start'); super.init (); print (' TestMixin2 init end');} class BaseClass {void init () {print ('Base init');} BaseClass () {init () } class TestClass extends BaseClass with TestMixin, TestMixin2 {@ override void init () {print ('TestClass init start'); super.init (); print (' TestClass init end');}} void main () {TestClass (); / / TestClass init start / / TestMixin2 init start / / TestMixin init start / / Base init / / TestMixin init end / / TestMixin2 init end / TestClass init end}

A little bit around, you can see that this has actually achieved the effect of multiple inheritance, which is more troublesome to write and, to some extent, less error-prone (compared to C++). No, no, no. no, no, no. The best and most complex example in the source code-WidgetsFlutterBinding, is defined as follows:

Class WidgetsFlutterBinding extends BindingBase with GestureBinding, ServicesBinding, SchedulerBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding {} after reading this article, I believe you have a certain understanding of "what is the use of Flutter mixin in Android". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report