recharge 0.0.1

  • Readme
  • Changelog
  • Installing
  • 64

Recharge #

Recharge is a simple library to hot reload your Dart code on file changes. This is useful when you are building Dart CLIs or API services. It utilises Dart VM's hot reloading and incremental build capabilities through vm_service library. To use Recharge check out following examples and run your code like this:

dart --enable-vm-service main.dart

Hello world #

Run following code and then change print string and save. Recharge will detect file modification and reload code. To execute main again onReload callback of Recharge is used.

import 'package:recharge/recharge.dart';

// Build recharge. Execute main after reload.
var recharge = Recharge(
  path: ".",
  onReload: () => main(),
);

void main() async {
  // Initialize recharge
  await recharge.init();

  // Say hello. After running change this text
  // and save it again.
  print("Hello world!");
}

HTTP Server #

Here we don't execute main after reload because server will be running even after reload. And functions will be replaced. So make any change to handle function and save. Refresh http://localhost:8080 in browser and you should see the change.

import 'dart:io';
import 'package:recharge/recharge.dart';

// Build recharge. Not using callback because after
// code change functions will be replaced. Execution will
// happen when next time request hits the server.
var recharge = Recharge(path: ".");

void main() async {
  // Initialize recharge
  await recharge.init();

  // Simple HTTP server
  var server = await HttpServer.bind("localhost", 8080);
  await for (var request in server) {
    handle(request, request.response);
  }
}

// Say hello to everyone
void handle(HttpRequest req, HttpResponse res) {
  res.write("Hello there!");
  res.close();
}

Decoupling #

If you are writing a serious project you want Recharge separate from your main code. You can create another main_debug.dart like below and use that while developing instead.

import 'package:recharge/recharge.dart';
import 'main.dart' as realMain;

// Build recharge. Execute main after reload.
var recharge = Recharge(
  path: ".",
  onReload: () => main(),
);

void main() async {
  // Initialize recharge
  await recharge.init();

  // Execute actual main function
  realMain.main();
}

Why #

It was initially written in early 2018 when I was building a code generator for a Flutter project. For every modification I had to run the code again with Dart CLI. Since it is a cold start of VM for every execution it was really slow. So I looked at Flutter source code and figured out how hot reload is done and coupled it with watcher library. Fast forward to 2020 and I was making a Youtube video about stateful hot reloading and thought why not make this a library. So here it is.

[0.0.1] #

  • Initial release with basic features

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  recharge: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:recharge/recharge.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
38
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Document public APIs. (-1 points)

7 out of 7 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/recharge.dart. (-0.50 points)

Analysis of lib/recharge.dart reported 1 hint:

line 76 col 19: Avoid empty catch blocks.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (vm_service).

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and recharge.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
vm_service ^2.1.4 2.3.1 4.0.0
watcher ^0.9.7+13 0.9.7+14
Transitive dependencies
async 2.4.1
collection 1.14.12
meta 1.1.8
path 1.6.4
pedantic 1.9.0