[Part 29 of the Flutter Problem Series] MainActivity cannot be converted to FlutterEngine, GeneratedPluginRegistrant.r

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

This is the [29th article of the Flutter problem series]. If you find it useful, please pay attention to the column.

One: problem description

When using Flutter and Android mixed development, I need to get the current battery of the mobile phone. When the MainActivity class rewrites the onCreate method of the FlutterActivity class, I wrote it like this (irrelevant code has been omitted)

 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   GeneratedPluginRegistrant.registerWith(this);
}
复制代码

After the program starts, an error is reported, as shown in the following figure

insert image description here

It means that there is an incompatible type, MainActivity cannot be converted to FlutterEngine.

Two: Solutions

At this time, this refers to MainActivity, and what we need is FlutterEngine, so we can't directly pass this as a parameter to the method, we should instantiate a FlutterEngine, and then pass this in, as shown in the following code.

 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));
}
复制代码

Has your problem been resolved? Welcome to leave a message in the comment area.

Give someone a rose, and there is a lingering fragrance in your hand. If you think the article is good, I hope you can give a one-click three-link, thank you.


concluding remarks

Google's Flutter is getting more and more popular. As of July 11, 2021, the GitHub star has reached 124K. Flutter is resolutely a trend, so as a front-end developer, there is no reason not to learn as soon as possible.

Whether you are a Flutter novice or have already started, you might as well pay attention first. I will write about the common components in Flutter (including source code analysis, component usage and precautions) and possible problems in the rare earth nuggets blog. , I hope that while learning by myself, I can also help more people.

Guess you like

Origin juejin.im/post/7082348918696050701