Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flutter Best Practices

Flutter Best Practices

This section will introduce best practices and coding standards in Flutter development.


Code Standards

Naming Convention

Type Rule Example
Class Names Upper Camel Case MyHomePage
Functions/Variables Lower Camel Case userName, getData
Constant Upper Camel Case + k prefix kMaxCount
file Lowercase + underscore my_home_page.dart

Example: Correct Naming

// Class names - Upper Camel Case

class

UserProfilePage

extends

StatelessWidget

{

}

// Functions - Lower Camel Case

void

fetchUserData

(

)

{

}

// Variables - Lower Camel Case

String userName

=

'Zhang San'

;

// Constants - k prefix

const

int

kMaxRetryCount

=

3

;

const

String kAppName

=

'My App'

;

// Enums - Upper Camel Case

enum AppState

{

loading

,

success

,

error

}

// File names - lowercase with underscores

// user_profile_page.dart


Performance optimization

1. Avoid unnecessary rebuilds

Example: Using const Constructors

// Recommended: use const to reduce rebuilds

const

Text

(

'Hello'

)

             

// Will not rebuild

const

SizedBox

(

width

:

10

)

// Avoid: creates new objects on every build

Text

(

'Hello'

)

                 

// Will rebuild

// Use const in lists

ListView

(

  children

:

const

[

    Text

(

'A'

)

,

    Text

(

'B'

)

,

    Text

(

'C'

)

,

 

]

,

)

2. Use keys to optimize list performance

Example: Using keys Correctly

// Add a unique key to list items

ListView.

builder

(

  itemCount

:

items.

length

,

  itemBuilder

:

(

context

,

index

)

{

   

return

ListTile

(

      key

:

ValueKey

(

items

[

index

]

.

id

)

,

 

// Use a unique identifier as the key

      title

:

Text

(

items

[

index

]

.

name

)

,

   

)

;

 

}

,

)

3. Lazily load list items

Example: Using ListView.builder

// Recommended: use builder for lazy loading

ListView.

builder

(

  itemCount

:

1000

,

 

// Must use for large lists

  itemBuilder

:

(

context

,

index

)

{

   

return

ListTile

(

title

:

Text

(

'Item $index'

)

)

;

 

}

,

)

// Avoid: using children to build large lists

ListView

(

  children

:

List.

generate

(

1000

,

(

i

)

=>

Text

(

'Item $i'

)

)

,

)


Project Structure

Example: Recommended Project Structure

// lib/

// ├── main.dart # entry file

// ├── app.dart # app configuration

// ├── core/ # core functionality

// │ ├── constants/ # constants

// │ ├── theme/ # theme

// │ ├── utils/ # utilities

// │ └── extensions/ # extensions

// ├── data/ # data layer

// │ ├── models/ # data models

// │ ├── repositories/ # repositories

// │ └── services/ # network services

// ├── domain/ # domain layer

// │ └── entities/ # entities

// ├── presentation/ # presentation layer

// │ ├── pages/ # pages

// │ ├── widgets/ # widgets

// │ └── providers/ # state management

// └── routes/ # routes


Error handling

Example: Global Error Handling

void

main

(

)

{

 

// Global exception capture

  FlutterError.

onError

=

(

details

)

{

   

// Log errors

    print

(

'Flutter Error: ${details.exception}'

)

;

   

// Report errors to the server

    reportError

(

details.

exception

)

;

 

}

;

 

// Zone exception capture

  runZonedGuarded

(

(

)

{

    runApp

(

const

MyApp

(

)

)

;

 

}

,

(

error

,

stackTrace

)

{

    print

(

'Zone Error: $error'

)

;

    reportError

(

error

)

;

 

}

)

;

}

Following best practices makes code easier to maintain, improves performance, and reduces bugs in production.