I try to create an abstract class to create a responsive application, I follow someone in youtube, the person in the the video is using the same code as I do, but in my case I got an error in the constructor that said:
The parameter 'mobile' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
I tried to change the mobile to optional final Widget? mobile; but in the condition build method I got another error:
The return type 'Widget?' isn't a 'Widget', as required by the closure's context.
Code:
import 'package:flutter/material.dart';
class ResponsiveWidget extends StatelessWidget {
final Widget mobile;
final Widget tablet;
final Widget desktop;
const ResponsiveWidget({Key? key, this.mobile, this.tablet, this.desktop})
: super(key: key);
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraint) {
if (constraint.maxWidth < 768) {
return mobile; // <----------- second error here
} else {
return desktop;
}
});
}
}
and then we can use:
body: SafeArea(
child: ResponsiveWidget(
mobile: Column(
children: [buildContainer(), buildText()],
),
desktop: Row(
children: [buildContainer(), buildText()],
),
),
),
More details:
Even when I try to check if my widget is null or not and return a default widget for example :
return this.mobile != null ? mobile : Container();
I got an error:
The return type 'Widget?' isn't a 'Widget', as required by the closure's context.
Is there any trick to avoid this issue please ?