Errors following the sample code for Flutter SDK


Userlevel 2
Badge

I posted here: https://stackoverflow.com/questions/72298875/getting-errors-integrating-amplitude-flutter-package-into-flutter

Here is what I posted:

I used the following reference code from the Amplitude team: [https://developers.amplitude.com/docs/flutter-setup][1]

Here is the relevant portion of my code:

    import 'package:flutter/material.dart';
import 'package:amplitude_flutter/amplitude.dart';
import 'package:amplitude_flutter/identify.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// Create the instance
final Amplitude analytics = Amplitude.getInstance(instanceName: "Assembly");

// Initialize SDK
analytics.init(a46xxxxxxxxxxxxxxxx87e);

// Log an event
analytics.logEvent('MyApp startup', eventProperties: {
'friend_num': 10,
'is_heavy_user': true
});

   

HOWEVER, when running on my local Mac, I get the following errors:

    lib/main.dart:16:5: Error: The name of a constructor must match the name of the enclosing class.
analytics.init(a46xxxxxxxxxxxxxxxx87e);
^^^^^^^^^
lib/main.dart:19:24: Error: Expected an identifier, but got ''Assembly''.
Try inserting an identifier before ''Assembly''.
analytics.logEvent('Assembly', eventProperties: {
^^^^^^^^^^
lib/main.dart:19:51: Error: Non-optional parameters can't have a default value.
Try removing the default value or making the parameter optional.
analytics.logEvent('Assembly', eventProperties: {
^
lib/main.dart:19:5: Error: The name of a constructor must match the name of the enclosing class.
analytics.logEvent('Assembly', eventProperties: {
^^^^^^^^^
lib/main.dart:13:43: Error: Method invocation is not a constant expression.
final Amplitude analytics = Amplitude.getInstance(instanceName: "Assembly");

 

There were a few inconsistencies in the documentation.

  1. Under initialize, it sets it up Amplitude.getInstance().init(a4638276fed4b8ea5c5a9ed13f07987e);
  2. But then it says to initialize the following way within the sample code: 

    // Create the instance
    final Amplitude analytics = Amplitude.getInstance(instanceName: "project");

    // Initialize SDK
    analytics.init(widget.apiKey);

    It’s unclear to me which is the way to reference the instance (among other things) based on the code example.

 

  1.  

  2.  [1]: https://developers.amplitude.com/docs/flutter-setup


2 replies

Userlevel 6
Badge +8

Hi @assembly ,

 

Did you utilize both examples? I would use the one Amplitude provides https://developers.amplitude.com/docs/flutter-setup#initialization. These issues are usually hard to debug as it is usually an implementation issue. Can you try to initialize the SDK as Amplitude have outlined in their documentation and see if that helps? I would aslo try to log a simple event without any user or event properties as that usually helps too!

Userlevel 6
Badge +9

Thank you, Denis! This is helpful. I also noticed some progress in this thread and I thought I would close the loop. It’s great to see the community getting together and supporting one another 😃

 

Reply