skip to Main Content

In a Flutter application, I’m trying to define my own view_cart event with custom parameters. So I decided to copy some of the code pieces from firebase_analytics library in order to send data successfully.

Here is the filterOutNulls function from firebase_analytics library:

  Map<String, Object> filterOutNulls(Map<String, Object?> parameters) {
    final Map<String, Object> filtered = <String, Object>{};
    parameters.forEach((String key, Object? value) {
      if (value != null) {
        filtered[key] = value;
      }
    });
    return filtered;
  }

Here is the _marshalItems function from firebase_analytics library.

  List<Map<String, dynamic>>? _marshalItems(List<AnalyticsEventItem>? items) {
    if (items == null) return null;

    return items.map((AnalyticsEventItem item) => item.asMap()).toList();
  }

This is my logEvent function:

      await FirebaseAnalytics.instance.logEvent(
        name: 'view_cart',
        parameters: filterOutNulls(<String, Object?>{
          "user_id": userId,
          "currency": convertSymbol(dvz),
          "value": totalPrice,
          "items": _marshalItems(list),
        }),
      );
// list variable is type of List<AnalyticsEventItem>

Finally, output of terminal:

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: 'package:firebase_analytics/src/firebase_analytics.dart': Failed assertion: line 115 pos 9: 'value is String || value is num': 'string' OR 'number' must be set as the value of the parameter: items
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2      FirebaseAnalytics.logEvent.<anonymous closure> (package:firebase_analytics/src/firebase_analytics.dart:115:9)
#3      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#4      FirebaseAnalytics.logEvent (package:firebase_analytics/src/firebase_analytics.dart:113:17)

What I don’t understand that the variables inside logEvent function is exact copy of logViewCart function. Even though they are same, logEvent function throws error but logViewCart does not.

Can anyone explain me where I did wrong?

2

Answers


  1. Chosen as BEST ANSWER

    I opened an issue in FlutterFire Github (check this out) and I saw that what I wanted to do is not supported by Google. It is not possible to assign a List of Objects to items parameter in custom logEvent.

    So I have to stick with pre-defined logging functions that Firebsase Analytics allows.


  2. Use This Method

     static void customLogEvent({
            required final String key,
            required final Map<String, dynamic> parameters,
          }) async {
            await FirebaseAnalytics.instance.logEvent(
              name: key,
              parameters: parameters,
            );
          }
    

    you Can add List to the Value of the Map

    List<Student> students = [
        Student(name: "John", rollno: "12", age: "26", marks: [23, 45, 35]),
        Student(name: "Krishna", rollno: "12", age: "26", marks: [23, 45, 35]),
        Student(name: "Rahul", rollno: "12", age: "26", marks: [23, 45, 35])
    ];
         
    var studentsmap = students.map((e){
            return {
                    "name": e.name, 
                    "rollno": e.rollno, 
                    "age": e.age, 
                    "marks": e.marks
                };
        }).toList();
    print(studentsmap);
    

    Output

    [
     {name: John, rollno: 12, age: 26, marks: [23, 45, 35]}, 
     {name: Krishna, rollno: 12, age: 26, marks: [23, 45, 35]}, 
     {name: Rahul, rollno: 12, age: 26, marks: [23, 45, 35]}
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search