GithubHelp home page GithubHelp logo

flutter_hotreloader's Introduction

title date categories keywords
How to automatically hot reload Flutter when Dart source files change
2018-12-01T23:54:48.507Z

Flutter hotreloader

Automatically reloads or restarts flutter run session(s)

Run

  1. Run as manu flutter sessions as you like with --pid-file /tmp/flutterXXX.pid option, where XXX might be anything, but different for each session.
  2. Start the hotreload.sh script

The hotreloader.sh helper script receives the full path of the changed file as the first argument. You may want to adjust the regular expression to match your paths so that hotreloader knows when to hot reload and when to hot restart the flutter. My version hot restarts every time the path contains .../state/....

Original Medium post from my blog

brew install entr or use your OS package manager. Or go to http://eradman.com/entrproject/

flutter run -d <your device id> --pid-file /tmp/flutter.pid

cd yourprojectdir
find lib/ -name '*.dart' | \    entr -p kill -USR1 $(cat /tmp/flutter.pid)

Bingo.

What does it do: entr reads a list of files from standard input and starts watching them for changes. Once it detects the change it executes a command passed to its command line. Flutter reacts to SIGUSR1 signal and hot reloads.

Although there’s a drawback in this simplistic approach: if you add a new Dart file and start making changes to it, the script above will not notice it. Because the list of files is created once at the start of the script and then fed to entr which then monitor them. If you need free white glove new files delivery do the following:

while true  
do  
    find lib/ -name '*.dart' | \  
        entr -d -p kill -USR1 $(cat /tmp/flutter.pid)  
done

The option -d will make entr exit every time a new file is detected in all the directories fed to it in the first place (in this case within lib/ hierarchy). And then the while loop will just restart the process all over again, scanning all the files and adding them to entr.

I actually use a more complex setup, because I use Redux in the project and hot reload doesn’t work good with changes to some redux related files (like middleware and such). So I call hot restart (SIGUSR2) for files related to state, and hot reload (SIGUSR1) for the rest.

Instead of kill I use a script, called hotreloader.sh which determines what type of file was changed and does the proper kill to cause either hot restart or hot reload

#!/bin/bash  
set -euo pipefail  
PIDFILE="/tmp/flutter.pid"

if [[ "${1-}" != "" && -e $PIDFILE ]]; then  
    if [[ "$1" =~ \/state\/ ]]; then  
        kill -USR2 $(cat $PIDFILE)  
    else  
        kill -USR1 $(cat $PIDFILE)  
    fi  
fi

And then I use the variation of the command above to start entr :

while true
do
    find lib/ -name '*.dart' | \
        entr -d -p ./hotreloader.sh /_
done

flutter_hotreloader's People

Contributors

kika avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.