Class not found error for a custom Android widget in a Flutter project

97 Views Asked by At

I'm a beginner in Flutter, and I'm attempting to create a custom Android widget within a Flutter project. I'm encountering an error "java.lang.RuntimeException: Unable to instantiate receiver com.example.flutter_ure.ClockWidget" when I try to select my widget. Here's what I've already checked and done:

  1. I have created a class ClockWidget that extends AppWidgetProvider. The class is located at android/app/src/main/java/com/example/flutter_ure/ClockWidget.java.
  2. I have defined the receiver in my AndroidManifest.xml file as follows:
<receiver
    android:name="com.example.flutter_ure.ClockWidget"
    android:label="Clock"
    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_clock" />
</receiver>

  1. I have ensured that the package specified in my AndroidManifest.xml matches that in my Flutter project (com.example.flutter_ure).
  2. The ClockWidget.java file includes the correct import for the R class:
import com.example.flutter_ure.R;

However, the error persists, and I'm unable to make my custom widget work. What am I doing wrong? How can I resolve this issue?

I would appreciate any help or advice to resolve this error. Thank you in advance.

-- Edit: added a github repository for anyone who wants to play with it: https://github.com/motumboe/flutter_ure

1

There are 1 best solutions below

3
On

According to the documentation, the name of the receiver should be the name of your widget class, and not the package to the class.

For example,

<receiver android:name="ExampleAppWidgetProvider"
                 android:exported="false">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info" />
</receiver>

So, in your case, it would look like this:

<receiver
    android:name="ClockWidget"
    android:label="Clock"
    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_clock" />
</receiver>