I have one code for mobile and web app in Flutter. Only difference is using another method for navigation back (because some issue in flutter web).
- For web: js.context.callMethod(‘back’)
- For mobile: Navigator.pop(context);
import 'package:flutter/material.dart';
import 'dart:js' as js; ///this is only for web
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("Homepage"),
),
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("Second page"),
),
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pop(context); /// for mobile
js.context.callMethod('back'); /// for web
},
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
I can’t build app for mobile using ‘dart:js’ and having method js.context.callMethod(‘back’) in code. Even if I have condition for platform.
What is good approach to do it?
2
Answers
I’d use conditional imports. Put your web-only stuff on one side, and your non-web on the other.
Dart provides a conditional import/export pattern, which is ideal for handling platform-specific code in Flutter apps. This can be applied to your scenario where you need different navigation methods for mobile and web.
Directory Structure
To start, you can organize your files like this:
Implementation
back_none.dart (for unsupported platforms)
This stub implementation ensures that if a platform is not supported, an error will be thrown, making it clear that the function is unavailable.
back_io.dart (for mobile platforms)
This implementation uses
Navigator.pop
for mobile platforms, which handles the back navigation properly.back_web.dart (for web platforms)
This implementation uses JavaScript’s
window.history.back()
to handle navigation in web browsers.back.dart (for conditional export)
This file conditionally exports the correct implementation based on the platform. The
if
directives ensure that only the relevant file for each platform is used.Usage
Now, in the files where you want to use the
back
function, simply import it:This approach ensures that your app runs on both mobile and web platforms without errors, as the appropriate navigation method will be selected based on the platform.