Skip to content

[pigeon] default member fields values are ignored after code generation #80048

Closed
flutter/packages
#5355

Description

@MatrixDev

Pigeon ignores default values for member variables and generated classes for all backends (Flutter, Android, iOS) doesn't have those default values.

Steps to reproduce

Pigeon class

class Test {
  var intResult = 20;
  var boolResult = true;
}

Generated class

class Test {
  int intResult;     // nothing here
  int boolResult;    // nothing here

  Object encode() {
    final Map<Object, Object> pigeonMap = <Object, Object>{};
    pigeonMap['intResult'] = intResult;
    pigeonMap['boolResult'] = boolResult;
    return pigeonMap;
  }

  static CreateConnectorMessage decode(Object message) {
    final Map<Object, Object> pigeonMap = message as Map<Object, Object>;
    return CreateConnectorMessage()
      ..intResult = pigeonMap['intResult'] as int
      ..boolResult = pigeonMap['boolResult'] as bool;
  }
}

Actual result

  • members in generated classes doesn't have default values

Expected result

  • members in generated classes must be initialised with default values when specified

Activity

  1. changed the title [-][pigeon] default arguments are ignored after code generation[/-] [+][pigeon] default member fields values are ignored after code generation[/+] on Apr 8, 2021
  2. TahaTesser commented on Apr 9, 2021

    @TahaTesser
    Contributor

    Hi @MatrixDev
    Thanks for filing the issue, I can reproduce the issue

    import 'dart:async';
    import 'dart:typed_data' show Uint8List, Int32List, Int64List, Float64List;
    
    import 'package:flutter/services.dart';
    
    class Version {
      String? string = "some version";
    }

    after
    flutter pub run pigeon \ --input pigeons/messages.dart \ --dart_out lib/messages.dart \ --objc_header_out ios/Classes/messages.h \ --objc_source_out ios/Classes/messages.m \ --objc_prefix FLT \ --java_out android/src/main/java/com/example/pigeon_plugin/Messages.java \ --java_package "com.example.pigeon_plugin"

    class Version {
      String? string;
    
      Object encode() {
        final Map<Object?, Object?> pigeonMap = <Object?, Object?>{};
        pigeonMap['string'] = string;
        return pigeonMap;
      }
    
      static Version decode(Object message) {
        final Map<Object?, Object?> pigeonMap = message as Map<Object?, Object?>;
        return Version()
          ..string = pigeonMap['string'] as String?;
      }
    }
    dev_dependencies:
      flutter_test:
        sdk: flutter
      pigeon: ^0.2.0

    https://github.com/gaaclarke/pigeon_plugin_example needs to be updated for null safety changes

  3. added
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    packageflutter/packages repository. See also p: labels.
    p: pigeonrelated to pigeon messaging codegen tool
    and removed
    in triagePresently being triaged by the triage team
    on Apr 9, 2021
  4. gaaclarke commented on Jun 14, 2021

    @gaaclarke
    Member

    This is works as designed. It wasn't implemented as of yet since we're being very careful about addressing the core need. This is a good idea as we consider new features.

  5. added
    P2Important issues not at the top of the work list
    on Jun 14, 2021
  6. exaby73 commented on May 13, 2022

    @exaby73
    Member

    Hello. As of testing this with version 3.0.3, pigeon throws the following error:

    Error: pigeons/messages.dart:2: Initialization isn't supported for fields in Pigeon data classes ("String? string = "some version";"), just use nullable types with no initializer (example "int? x;").
    

    Tested this on both stable and master channels of Flutter

    Code sample used
    class Version {
      String? string = "some version";
    }
    flutter doctor -v (Stable)
    [✓] Flutter (Channel stable, 3.0.0, on macOS 12.3.1 21E258 darwin-arm, locale en-US)
        • Flutter version 3.0.0 at /Users/nabeelparkar/fvm/versions/stable
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ee4e09cce0 (7 days ago), 2022-05-09 16:45:18 -0700
        • Engine revision d1b9a6938a
        • Dart version 2.17.0
        • DevTools version 2.12.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
        • Android SDK at /Users/nabeelparkar/Library/Android/sdk/
        • Platform android-32, build-tools 32.1.0-rc1
        • ANDROID_SDK_ROOT = /Users/nabeelparkar/Library/Android/sdk/
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • CHROME_EXECUTABLE = /Applications/Brave Browser.app/Contents/MacOS/Brave Browser
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2022.1.1)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 67.1.4
        • Dart plugin version 221.5591.52
    
    [✓] VS Code (version 1.67.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.40.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 12.3.1 21E258 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Brave Browser 101.1.38.115
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    flutter doctor -v (Master)
    [✓] Flutter (Channel master, 3.1.0-0.0.pre.733, on macOS 12.3.1 21E258 darwin-arm, locale en-US)
        • Flutter version 3.1.0-0.0.pre.733 at /Users/nabeelparkar/fvm/versions/master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision c60936e5c9 (14 hours ago), 2022-05-16 13:04:12 -0400
        • Engine revision aeab8402c6
        • Dart version 2.18.0 (build 2.18.0-117.0.dev)
        • DevTools version 2.13.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
        • Android SDK at /Users/nabeelparkar/Library/Android/sdk/
        • Platform android-32, build-tools 32.1.0-rc1
        • ANDROID_SDK_ROOT = /Users/nabeelparkar/Library/Android/sdk/
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.3.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • CHROME_EXECUTABLE = /Applications/Brave Browser.app/Contents/MacOS/Brave Browser
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2022.1.1)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 67.1.4
        • Dart plugin version 221.5591.52
    
    [✓] VS Code (version 1.67.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.40.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 12.3.1 21E258 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Brave Browser 101.1.38.115
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!```
    
    </details>
    
  7. github-actions commented on Dec 2, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  8. locked as resolved and limited conversation to collaborators on Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityfound in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions