Flutter: Conditional Import for Web and Native

The Rivepod way to provide platform specific implementations of a midi player

lib/midi_player_native.dart:5:8: Error: Not found: 'dart:js'
import 'dart:js' as js;
Restarted application in 175ms.
2021-01-01 17:56:25.423 INFO: MyApp: build loglevel: FINE
2021-01-01 17:56:25.425 FINE: MidiPlayerWeb: MidiPlayerWeb.init
2021-01-01 17:56:25.459 FINE: MyHomePage: build
2021-01-01 17:56:44.765 FINE: MyHomePage: _playMidi
2021-01-01 17:56:44.766 FINE: MidiPlayerWeb: play midi 60
2021-01-01 17:56:44.767 FINE: MidiPlayerWeb: playNote C4
2021-01-01 17:56:44.978 FINE: MidiPlayerWeb: play midi 64
2021-01-01 17:56:44.979 FINE: MidiPlayerWeb: playNote E4
2021-01-01 17:56:45.187 FINE: MidiPlayerWeb: play midi 67
2021-01-01 17:56:45.187 FINE: MidiPlayerWeb: playNote G4
Xcode build done.                                           24,5s
Connecting to VM Service at ws://
flutter: 2021-01-01 17:51:25.901051 INFO: MyApp: build loglevel: FINE
flutter: 2021-01-01 17:51:25.934127 FINE: MidiPlayerNative: MidiPlayerNative.init
flutter: 2021-01-01 17:51:25.936137 FINE: MidiPlayerNative: Loading File... assets/Nylon Guitar.sf2
flutter: 2021-01-01 17:51:26.459887 FINE: MyHomePage: build
flutter: Result: Prepared Sound Font
flutter: 2021-01-01 17:51:33.826726 FINE: MyHomePage: _playMidi
flutter: 2021-01-01 17:51:33.827214 FINE: MidiPlayerNative: play midi 60
flutter: 2021-01-01 17:51:34.029797 FINE: MidiPlayerNative: play midi 64
flutter: 2021-01-01 17:51:34.236014 FINE: MidiPlayerNative: play midi 67
/* would be useful if this works in a future version of Dart :-)
const useWeb = true;

import 'midi_player_native.dart';
import 'midi_player_web.dart';

Software Developer for more than 35 years, started with C and C++, after Java, Python, Groovy, Lua, Golang and Swift now focussing on Dart and Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store