Flutter: Adding Linux App

I saw a flurry of articles on Google Now and OMG Ubuntu about Flutter adding the ability to compile to native Linux apps. Since I have been playing with Flutter, I decided to give it a try. I won’t duplicate what they have written. Instead, this article is my notes on enabling my app to work natively on Linux.

The first step was running the command to enable the Linux app:

flutter config --enable-linux-desktop

The messages mentioned that I may want to restart my IDE. I’m using Visual Studio Code, so I just closed and reopened that.

I’m supposed to be able to start a debug version of the program with:

flutter run -d linux

That didn’t work. I got this message:

No devices found with name or id matching 'linux'

I saw a note that said to run this command on existing projects. It appears to “recreate” the project adding in the desktop support.

flutter create .

That didn’t work either. It gave me this error message:

Ambiguous organization in existing files: {com.example, new.digitaleagle}. The --org command line argument must be specified to recreate project.

So, I added the “–org” like the message said.

flutter create --org new.digitaleagle .

That seemed to work. It gave me this output:

Recreating project ....
  ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (created)
  ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings (created)
  ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (created)
  ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings (created)
  android/app/src/main/kotlin/new/digitaleagle/matthew/MainActivity.kt (created)
Wrote 8 files.

All done!
[✓] Flutter: is fully installed. (Channel stable, v1.17.4, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices: is fully installed. (Android SDK version 29.0.3)
[!] Android Studio: is partially installed; more components are available. (version 3.6)
[✓] VS Code: is fully installed. (version 1.47.1)
[!] Connected device: is not available.

Run "flutter doctor" for information about installing additional components.

In order to run your application, type:

  $ cd .
  $ flutter run

Your application code is in ./lib/main.dart.

Well, if I can’t run the debug version, maybe I can build a full version. I did that with this command:

flutter build linux

Again, that didn’t work! It said Linux was not supported.

Downloading linux-x64/linux-x64-flutter-glfw tools...               2.3s
Downloading linux-x64/flutter-cpp-client-wrapper-glfw tools...         0.3s
"build linux" is not currently supported.

This made me wonder about the flutter version. I tried to upgrade…

flutter upgrade

That didn’t work. I decided to check the version. I think the issue is not necessarily the version, but the fact that I have the stable channel.

flutter --version

Here’s the output:

Flutter 1.17.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8af6b2f038 (3 weeks ago) • 2020-06-30 12:53:55 -0700
Engine • revision ee76268252
Tools • Dart 2.8.4

I attempted to change it to the master channel with:

flutter channel master
flutter upgrade

Then, I could add the desktop app

flutter config --enable-linux-desktop

Then, I tried to run it

flutter run -d linux

I still got an error message:

Exception: No Linux desktop project configured. See https://github.com/flutter/flutter/wiki/Desktop-shells#create to learn about adding Linux support to a project.

So, I tried the create one more time

flutter create --org new.digitaleagle .

Now, I’m getting somewhere. It still gave an error, but it is at least trying to run it.

flutter run -d linux

It looks like I’m missing ninja?

CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage           
Building Linux application...                                           
Exception: Unable to generate build files

So, I installed ninja-build:

sudo apt install ninja-build

After that, I tried again. It looks like I am missing clang.

Launching lib/main.dart on Linux in debug mode...
CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineCXXCompiler.cmake:48 (message):
  Could not find compiler set in environment variable CXX:              
                                                                        
  clang++.                                                              
                                                                        
Call Stack (most recent call first):                                    
  CMakeLists.txt:2 (project)                                            
                                                                        
                                                                        
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage           
Building Linux application...                                           
Exception: Unable to generate build files

So, I installed Clang:

sudo apt install clang

Then, it worked!! The app opened up.

Now, there are code errors. The local storage doesn’t work on the desktop like it does on Android.

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building Builder:
flutter: The method 'getString' was called on null.
flutter: Receiver: null

This is the code that is causing the problem:

SharedPreferences prefs;
prefs = await SharedPreferences.getInstance();
return prefs.getString("NehemiahToken");

It sounds like this is something that is not quite implemented yet. I’ll save fixing that for another post. I think I am going to have to save preferences to the file system instead.

Resources

Leave a Comment

Your email address will not be published. Required fields are marked *