why flutter web_view showing error on initial Url? - flutter

I install the web_view flutter plugin and set minTargetSDk to 20 but still, I face an error on initialurl.
Here is the code snippet:

Kindly change you page name from WebView to any other.
Or you can do it as below:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart' as wv;
class WebView extends StatefulWidget {
const WebView({Key? key}) : super(key: key);
#override
State<WebView> createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
#override
Widget build(BuildContext context) {
return wv.WebView(
initialUrl: 'https://flutter.dev/',
);
}
}

Related

How to open a specific screen when local notification is shown in Flutter? app is closed

I scheduled notification locally,I close the app(completely) and when the notification appears. I click on it and my app opens normally on "homepage"(same as clicking app icon to open it). I want to open a specific page ,lets say "page2()"
*** using the package : flutter_local_notifications: ^13.0.0
use FirebaseMessaging.instance.getInitialMessage() to handle message in background
Example:
void handleMessageOnBackground() {
FirebaseMessaging.instance.getInitialMessage().then(
(remoteMessage) {
if (remoteMessage != null) {
String payload = json.encode(remoteMessage.data);
//navigator two orther screen
}
},
);
}
And init handleMessageOnBackground() in home_screen.dart , not main.dart
main.dart > splash_screen > home_screen
home_screen.dart
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
#override
void initState() {
super.initState();
handleMessageOnBackground();
}
#override
Widget build(BuildContext context) {
return Container();
}
}

The method 'useEffect' isn't defined for the type

I'm new to flutter and I want to implement the useEffect hook.
Here is my widget:
import 'dart:developer';
import 'package:flutter/material.dart';
class MarketRunnerChart extends StatefulWidget {
const MarketRunnerChart({Key? key}) : super(key: key);
#override
State<MarketRunnerChart> createState() => _MarketRunnerChartState();
}
class _MarketRunnerChartState extends State<MarketRunnerChart> {
#override
Widget build(BuildContext context) {
useEffect(() {
log('okok');
}, []);
return Text("Some text");
}
}
But I got the error The method 'useEffect' isn't defined for the type '_MarketRunnerChartState'.
When I remove the useEffect hook out of the build function and put it directly in the class I got error 'useEffect' must have a method body because '_MarketRunnerChartState' isn't abstract.
I'm used to work with React, but right now with flutter I can't figure out how to implement that hook.
How am I supposed to do this ?
try add
import 'package:flutter_hooks/flutter_hooks.dart';
on top of your class file
import flutter hooks
import 'package:flutter_hooks/flutter_hooks.dart';
class MarketRunnerChart extends StatefulWidget {
const MarketRunnerChart({Key? key}) : super(key: key);
#override
State<MarketRunnerChart> createState() => _MarketRunnerChartState();
}
class _MarketRunnerChartState extends State<MarketRunnerChart> {
useEffect(() {
print('your log');
}, []);
#override
Widget build(BuildContext context) {
return Text("Some text");
}
}
You can follow the doc example, import flutter_hooks, extend the HookWidget.
import 'package:flutter_hooks/flutter_hooks.dart';
class Example extends HookWidget {
const Example({Key? key, })
: super(key: key);
#override
Widget build(BuildContext context) {
//your variable/instance like to listen
useEffect(() {
log('okok');
}, [...listenThisInstance...]);
return Container();
}
}
More about useEffect

How to make a screen that show a specific page if there's data to display and show a specific page if there's no data to display

I made 3 dart file that called Cart, CartSection, and EmptyCartState.
here is the code from Cart
class CartPage extends StatefulWidget {
const CartPage({Key? key}) : super(key: key);
#override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
#override
Widget build(BuildContext context) {
return Scaffold();
}
}
it's still empty.
what I want to know is how to make this Cart show CartSection if there are something in the cart and show EmptyCartState if there is nothin in the cart?
While you like to get empty screen, you can use nullable data. I am using int as datatype, you can use your model in this case.
class CartPage extends StatefulWidget {
const CartPage({
Key? key,
this.data,
}) : super(key: key);
final int? data;
#override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
#override
Widget build(BuildContext context) {
return widget.data == null
? Text("empty data widget")
: Text("cart data widget");
}
}
Visit dart.dev to learn more about it.

Flutter access State<T> from it's StatefulWidget

I'm writing a Application, where Widgets and their State need to be saved to Disk and later be restored. In order to save a StatefulWidget I need to access it's corresponding State<T> object.
Here's how I imagined the code to look like:
class Block extends StatefulWidget {
const Block({Key? key}) : super(key: key);
void saveToDisk(){
// access BlockState object
// save to disk…
}
#override
BlockState createState() => BlockState();
}
class BlockState extends State<Block> {
final String _someState = ‚Hello Stackoverflow‘;
#override
Widget build(BuildContext context) {
return const Text(‚Some Text‘);
}
}
Does anybody know how to access the BlockState object (first comment in saveToDisk())?
widget.saveToDisk();
All stateful widgets have it this way.

Flutter State<WidgetName> createState() => WidgetNameState()

In Flutter, when initializing a new stateful widget, it is being initialized like this by default:
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
WidgetNameState createState() => WidgetNameState();
}
I have seen another way of initializing the statefulwidget with #override being slightly different.
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
State<WidgetName> createState() => WidgetNameState();
}
Notice in the #override method, WidgetNameState became State<WidgetName>. There is an explanation in the Flutter repo that explains it: Link, but I couldn't comprehend what it is trying to say.
What does State<WidgetName> do exactly? Does it give any advantages?
I thought it wouldn't be necessary as WidgetNameState already extends from State<WidgetName> in its class construction.
class WidgetNameState extends State<WidgetName> {
#override
Widget build(BuildContext context) {
Using the generic really let's you define an abstract Widget interface that must be used without having to define any state along with that abstract widget interface.
Let's look at using the concrete class first (WidgetNameState). This is an abstract definition, we must define the state if we do this.
abstract class FooWidget extends StatefulWidget {
const FooWidget({Key? key}) : super(key: key);
#override
_FooWidgetState createState();
}
abstract class _FooWidgetState extends State<FooWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now to be able to extend this you must extend both the widget and the state.
class ImplementedFooWidget extends FooWidget {
const ImplementedFooWidget({Key? key}) : super(key: key);
#override
_ImplementedFooWidgetState createState() => _ImplementedFooWidgetState();
}
class _ImplementedFooWidgetState extends _FooWidgetState {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now let's look at using the generic (State<WidgetName>). If we use the generic, we can just define and extend the widget and have our own custom state.
abstract class BarWidget extends StatefulWidget {
const BarWidget({Key? key, required this.someRequiredString})
: super(key: key);
final String someRequiredString;
#override
State<BarWidget> createState();
}
class ImplementedBarWidget extends BarWidget {
const ImplementedBarWidget({Key? key, required String someRequiredString})
: super(
key: key,
someRequiredString: someRequiredString,
);
#override
_ImplementedBarWidgetState createState() => _ImplementedBarWidgetState();
}
class _ImplementedBarWidgetState extends State<ImplementedBarWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
I hope that all makes sense.