'How can i pass a variable to a static function
I already have a question regarding this here but as the question was too complicated for a simple answer I created a small project just to showcase my problem.
For this project I have two files
global_variables.dart and main.dart
under global_variables.dart I just have one variable declared
String? currentFirebaseUser;
Then under my main.dart I have this
// ignore_for_file: deprecated_member_use
import 'dart:isolate';
import 'dart:ui';
import 'package:background_locator/background_locator.dart';
import 'package:background_locator/location_dto.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:static_function_test/global_variables.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const String _isolateName = "LocatorIsolate";
ReceivePort port = ReceivePort();
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(port.sendPort, _isolateName);
port.listen((dynamic data) {});
initPlatformState();
}
Future<void> initPlatformState() async {
_checkLocationPermission;
await BackgroundLocator.initialize();
}
Future<bool> _checkLocationPermission() async {
var permission = await Permission.location.status;
if (permission.isDenied) {
permission = await Permission.location.request();
}
if (permission.isGranted) {
return true;
} else {
return false;
}
}
static void backgroundLocationCallBack(LocationDto location) async {
// ignore: avoid_print
//I get currentFirebaseUser is null here
print("from background location+ ${currentFirebaseUser.toString()}");
}
void getLocationLiveUpdates() async {
currentFirebaseUser = 'pannam';
return await BackgroundLocator.registerLocationUpdate(
backgroundLocationCallBack,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
RaisedButton(
onPressed: getLocationLiveUpdates,
)
]),
),
);
}
}
I want to somehow pass the variable currentFirebaseUser = 'pannam'; to the function static void backgroundLocationCallBack(LocationDto location) async {} but the print statement in it always returns a null. so the question is how do I pass a variable to a static function from outside.?
PS: I can't modify the static function to add something like static void backgroundLocationCallBack(LocationDto location String id) because it is a part of a plugin hence can't and am unwilling to modify it.
plugins:
flutter_geofire: ^2.0.1
background_locator: ^1.6.12
permission_handler: ^9.2.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
