I'm trying to log user events using firebase_analytics I followed the documentation
this my main() method
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
name: "test_app",
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
This is MyApp class
class MyApp extends StatelessWidget {
const MyApp({super.key});
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
static FirebaseAnalyticsObserver observer =
FirebaseAnalyticsObserver(analytics: analytics);
@override
Widget build(BuildContext context) {
return
...
navigatorObservers: <NavigatorObserver>[observer],
routes: {
'/': (context) => const MainScreen(),
'product': (context) => ProductScreen(
analytics: analytics,
observer: observer,
),
},
),
);
}
}
And this is how I log event
body: TextButton.icon(
onPressed: () async {
await analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
'string': 'string',
},
);
},
icon: const Icon(Icons.analytics),
label: const Text("Test Analytics")),
why do I end up with this error
W/FA ( 7136): '' is an invalid event name.
CodePudding user response:
if you haven't imported Firebase Analytics
, import
import 'package:firebase_analytics/firebase_analytics.dart';
first in your main file.
If the issue still persists, run the following commands:
cd android
./gradlew clean
./gradlew build
flutter clean
flutter pub get
CodePudding user response:
I tried this and it works. Let me know if it works or what error you are facing.
final FirebaseAnalytics _analytics = FirebaseAnalytics();
Future sendAnalyticsEvent({String? aString}) async {
await _analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
'userData': aString,
},
);
}
and try to call this sendAnalyticsEvent
function whenever you want.