Home > Software engineering >  How get the flutter/dart build date time?
How get the flutter/dart build date time?

Time:10-07

I'm looking to display the build date of the Flutter/Dart app.

Is it possible ?

A possible solution for me it's to take Gradle res value : On Gradle/Android I can add this line in the build.gradle :

buildConfigField "long", "BUILD_TIME", System.currentTimeMillis()   "L"

But how I can get this in flutter part ?

CodePudding user response:

That would probably be a little bit complicated but still you can do that

  1. So, what you need is add your field into the gradle as you did in question

  2. Add next script

import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    new MethodChannel(this.getFlutterView(), "getFlavorConfiguration").setMethodCallHandler(new MethodChannel.MethodCallHandler() {
      @Override
      public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        result.success(BuildConfig.FLAVOR_CONFIGURATION);
      }
    });
  }
}

Where "getFlavorConfiguration" would be your new MethodChannel

  1. In it's callback emit your BuildConfig value
  2. And for iOS, modify AppDelegate.m like below and don't forget to add config fields to plist files
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
  FlutterMethodChannel * channel = [FlutterMethodChannel methodChannelWithName:@"getFlavorConfiguration"
                                                               binaryMessenger:controller];
  [channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
    NSString *flavorConfig = (NSString *) [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FlavorConfiguration"];
    result(flavorConfig);
  }];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
  1. Finally, Flutter code change :
  void initState() {
    super.initState();
    MethodChannel channel = MethodChannel("getFlavorConfiguration");
    channel.invokeMethod("method").then((result) {
      setState(() {
        _flavorConfig = result;
      });
    });
  }

In InvokeMethod you can put your string

  • Related