1
votes

i'm starting a flutter project with target android/ios/web, is there a way to write same lib code for mobile and web platform ? Didn't found solution like conditional import or something like that.

In my project, i have created a file 'global_import.dart' with all imports specifics for web and mobile like this:

//mobile packages
/*
export 'package:flutter/material.dart';
*/

//web packages
///*
export 'package:flutter_web/material.dart';
//*/

Instead of import immediatly flutter or flutter_web packages in my widget files, i import this file and comment/uncomment mobile/web packages as needed.

I found only this solution at the moment, looking for better one.

2

2 Answers

1
votes

Flutter version 1.9 they remove

import 'package:flutter_web/material.dart'

Now you can directy use package

import 'package:flutter/material.dart';

https://www.youtube.com/watch?v=wzNd3yyLcaU

Flutter web project now it can import all normal flutter import, no more special case need for developing web.

3
votes

Here's a code snippet I've found, might be useful.

export 'package:flutter_stub/material.dart'
  // ignore: uri_does_not_exist
  if (dart.library.html) 'package:flutter_web/material.dart'
  // ignore: uri_does_not_exist
  if (dart.library.io) 'package:flutter/material.dart';

https://github.com/aloisdeniel/flutter_shared_ui_poc/blob/master/packages/flutter_cross/lib/material.dart

Check out this repo from Aloïs Deniel for more info.