avatarsimbu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

949

Abstract

fix</pre></div><p id="8b9e">It will add underscore in front of variable a:</p><div id="39a9"><pre><span class="hljs-function">fn <span class="hljs-title">main</span>()</span> { <span class="hljs-keyword">let</span> _a = <span class="hljs-number">10</span>; }</pre></div><p id="7e4e">3 Clippy:</p><p id="9809">We can use it to catch common mistakes and improve your Rust code.</p><p id="1e04">To install clippy , by running:</p><div id="bb1c"><pre>rustup component <span class="hljs-keyword">add</span> clippy</pre></div><p id="3a18">Like we want to use PI value,</p><div id="7232"><pre><span class="hljs-function">fn <span class="hljs-title">main</span>()</span> { <span class="hljs-keyword">let</span> a = <span class="hljs-number">3.14</span>; <span class="hljs-keyword">let</span> b = <span class="hljs-number">2.0</span>; println!(<span class="hljs-string">"{}"</span>, a * b); }</pre></div><p id="d264">We run :</p><div id="58e4

Options

"><pre>cargo clippy</pre></div><p id="d654">It will tell us to use PI defined in standard library for more precise.</p><p id="f193">So we can change our code to :</p><div id="b409"><pre><span class="hljs-keyword">use</span> std::<span class="hljs-type">f32</span>::consts::PI;

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">a</span> = PI; <span class="hljs-keyword">let</span> <span class="hljs-variable">b</span> = <span class="hljs-number">2.0</span>; <span class="hljs-built_in">println!</span>(<span class="hljs-string">"{}"</span>, a * b); }</pre></div><p id="416f">4 rust-analyzer</p><p id="a872">It’s an extension, we can install it by searching rust-analyzer in extensions in vs code. It will make vs code gain abilities such as type definition, code completion, syntax highlighting and inline errors.</p></article></body>

Join Medium to view all the articles, or check out the code.

Flutter — Offline First

Offline is my default state, a connection is a nice addition

Christian Muehle

To support being Offline first, I’ve added a new feature that monitors the client device connection and updates a ‘Connection’ state with two values ‘Online’ & ‘Offline’.

Connection Detector — Story by example
Connection detection feature tests

In the next post in the mini ‘_Offline First_’ series, I will use the new connectivity detection, to point data requests at a local DB when offline, and to sync them when the online API is available.

Ta Da

Feature test report
Feature test log excerpt

It was easy to implement the feature using the connectivity_plus package.

It just required a new provider:

// Package imports:
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:digestable_prologue/connection_detector/model/network_connection_state.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class NetworkConnectionStateNotifier
    extends StateNotifier<NetworkConnectionState> {
  NetworkConnectionStateNotifier(super.state);

  setNetworkStateByConnectivityResult(ConnectivityResult connectivityResult) {
    var onlineStates = <ConnectivityResult>[
      ConnectivityResult.wifi,
      ConnectivityResult.ethernet,
      ConnectivityResult.mobile
    ];

    var isOnlineState = onlineStates.contains(connectivityResult);

    state = isOnlineState
        ? NetworkConnectionState.online
        : NetworkConnectionState.offline;
  }
}

final networkConnectionStateProvider = StateNotifierProvider<
    NetworkConnectionStateNotifier, NetworkConnectionState>(
  (ref) => NetworkConnectionStateNotifier(NetworkConnectionState.offline),
);

And an enum:

enum NetworkConnectionState {
  offline,
  online
}

And the connectivity_plus package with a single event bootstrap statement:

@override
void initState() {
   super.initState();
   ...
   subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult connectivityResult) {
      ref
          .read(networkConnectionStateProvider.notifier)
          .setNetworkStateByConnectivityResult(connectivityResult);
    });
}

XP

Test First Help to Improve and Simplify the Design

My sketched initial design was this:

Initial Design

It felt right to wrap the package calls with a service that could be overridden in test.

But when I started to code the bootstrapping and steps in the feature tests it got complex, especially given that GetConnectivity() method was an async future, it quickly felt like a code smell, bad, over complex code.

On top of that I realised that I didn’t need to use the new ConnectivityServiceProvider in the ‘_PrologueApp_’ widget.

Instead, I could simply listen to a connectivity change event via the package and just call the NetworkStateProvider and let it decide whether it was on or offline based on the ConnectivityResult.

BackBurner

flutter_gherkin step matching issue

This

Flutter gherkin runStep

Matched this

An iPhone step

And not this

An iPhone in Airplane mode step

Causing the test to fail because the network connectivity was left set to wifi!

Failing test

I will create a PR against flutter_gherkin to fix this.

To patch the issue I renamed ‘An iPhone’ to ‘An Apple iPhone’.

Sound & Vision

Portishead — Dummy

Links

- Packages

Flutter
Development
Mobile App Development
Programming
Mobile
Recommended from ReadMedium