1

Looks like Flutter for Web and Flutter for Mobile have to exist as separate projects due to the imports.

Example:

import 'package:flutter_web/material.dart

vs

import 'package:flutter/material.dart';

Is there anyway to build one flutter project with one code base that works for both web and mobile (ios/android)? If not, is this coming? If so, can you provide an example app?

Would like to just make one code base for the web and mobile and not have to maintain separate projects/code repos.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52

1 Answers1

2

The OP's question is a bit old and is no longer applicable at the time of posting(7/21/2020). Flutter now has consolidated web into the main flutter package, which prevents us from running into issues with imports like this. flutter_web is no longer a separate package.

However, you may have been able to accomplish this even at the time you posted your question with conditional imports. This answer provides an excellent method of doing this. The following are the essentials of that post:

The core idea is as follows.

  1. Create an abstract class to define the methods you will need to use in general.
  2. Create implementations specific to web and android dependencies which extends this abstract class.
  3. Create a stub which exposes a method to return the instance of this abstract implementation. This is only to keep the dart analysis tool happy.
  4. In the abstract class import this stub file along with the conditional imports specific for mobile and web. Then in its factory constructor return the instance of the specific implementation. This will be handled automatically by conditional import if written correctly.

This method allows for you to do these imports based on platform and applies to all packages that may not support every possible flutter platform(e.g. dart:html, dart:js, dart:js_util, dart:io).

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52