Reputation: 631
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.
Upvotes: 2
Views: 1679
Reputation: 14335
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.
Upvotes: 1
Reputation: 5385
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';
Check out this repo from Aloïs Deniel for more info.
Upvotes: 3