flutter | cookbook | dart

Flutter Cookbook

This cookbook will help you solve common problems while writing Flutter apps.

Abhith Rajan
Abhith RajanApril 30, 2018 · 3 min read · Last Updated:

Most of the solution implementations mentioned below can be found in my repo https://github.com/Abhith/piggy-flutter.

Move to a new screen without back

An example of this use case is the login page. When a user successfully logged in, we redirect the user to another page, and in most case, we don’t want a back button to go back to the login page. This can be easily done using Navigator.pushReplacement

    Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => HomePage()),
    );

State Management

Bloc

As a rookie in the flutter, one of the biggest challenges I faced was state management. Started with StatefulWidget’s setState, but sooner understood that it is not enough (state persistence was the tough part). After a long search, found that there are many ways to do it in flutter including Redux, Scoped Model, BLoC etc. I don’t like Redux too much, I tried it for React Native app earlier and moved to mobx-state-tree instead.

One I got excited about was Streams and BLoC (business logic components). But first found tutorials was not enough and heavily confusing (for a rookie). But tried them one by one and my solutions get better after each commit.

Now in my view, the best way to do it is by using the bloc package and the best part of it is, this one is very well documented with example applications. It also has an extension for VSCODE to ease the usage.

Build

Build failed - null value in entry: outputFile=null

Recently my machine shutdown unexpectedly during a build in progress. After that, all the build is failing with this error.

Removing the .gradle directory in the root project directory will fix the problem.

Error importing package:http/http.dart

After long time, when I tried to build, I got this error. To resolve,

Go to your pubspec.yaml file , and add the http dependency:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: any

Then run the following,

flutter packages get

If you are using VS Code as the IDE, the above will be automatically executed once you save the pubspec.yaml file. (If you installed the Flutter extension for VS Code).

SocketException: OS Error: Connection refused, errno = 111 when trying to connect to localhost

My ASPNET Core API’s where running on localhost and I was trying to request against the same using the Android emulator, then your server endpoint should be 10.0.2.2:PORT_NUMBER instead of localhost:PORT_NUMBER as AVD uses 10.0.2.2 as an alias to your host loopback interface (i.e) localhost.

More than one file was found with OS independent path ‘META-INF/proguard/androidx-annotations.pro’

Adding following in the android > app > build.gradle fixed the build error,

android {
     packagingOptions {
         exclude 'META-INF/proguard/androidx-annotations.pro'
     }
 }

Disable Null Safety Check

Include a version prior to 2.12 in the pubspec.yaml file to disable null safety.

environment:
  sdk: ">=2.11.0 <3.0.0"

Changelog

2022-03-09

  • Added solution for “Disable Null Safety Check”

2020-03-11

  • Added solution for “More than one file was found with OS independent path ‘META-INF/proguard/androidx-annotations.pro’""

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Abhith Rajan

Written byAbhith Rajan
Abhith Rajan is a software engineer by day and a full-stack developer by night. He's coding for almost a decade now. He codes 🧑‍💻, write ✍️, learn 📖 and advocate 👍.
Connect

Is this page helpful?

Related ArticlesView All

Related VideosView All

CheckboxListTile (Flutter Widget of the Week)

SwitchListTile (Flutter Widget of the Week)

ToggleButtons (Flutter Widget of the Week)