My code is very simple:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(title:"title", home: Scaffold(body: TextField()));
}
}
void main() {
runApp(MyApp());
}
No matter how I try to include the text field, the program just crushes.
I'm compiling to web, maybe that's related to the problem?
Edit: So that's awkward. But the code seems to work perfectly fine now...
Though the following code still crashes after adding the text field:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(title:"title", home: Scaffold(body:
Row(children: [
Column(children: [
TextField()
])
]
)));
}
}
void main() {
runApp(MyApp());
}
This happens, because there is no width constraint to the TextField.
In your case, wrapping the column in an Expanded widget should fix the problem.
See the flutter documentation for more info and solutions to this problem: https://flutter.dev/docs/testing/common-errors#an-inputdecoratorcannot-have-an-unbounded-width
Related
I'm having a problem with Flutter. More precisely, since it is not a language that I have a command of, I did something by looking at the videos. Of course, old videos etc. I've been in trouble.
First of all, I will write what I want to do and what I failed to achieve. I am pulling my website into application with webview. There is no problem here. Then I added onesignal. This also works flawlessly. But I want to get the id called playerid that onesignal defines for each device and post it to my php page. I tried many plugins for this, but I could not succeed. Then I sent it as a cookie, it still didn't work. I realized that I can send data as get in the latest webview link, but the fact that I can't pull the variable I created here also infuriated me. I'm adding the codes below and asking for help from an expert friend. If there is, you can suggest an easy way to POST and GET. Thanks everyone in advance.
In the code below, I can't get the variable that I defined as userid and printed in the link section. In the simplest way, if I do this, it can work for now.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';
void main() async {
runApp(PnlbaseApp());
}
class PnlbaseApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PNLBASE APP',
home: PnlbaseAppPage(),
);
}
}
class PnlbaseAppPage extends StatefulWidget {
PnlbaseAppPage({Key? key}) : super(key: key);
#override
_PnlbaseAppPageState createState() => _PnlbaseAppPageState();
}
class _PnlbaseAppPageState extends State<PnlbaseAppPage> {
#override
void initState() {
super.initState();
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setAppId("APP-ID");
OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
print("Accepted permission: $accepted");
});
OneSignal.shared.getDeviceState().then((deviceState) {
print("OneSignal: device state: ${deviceState?.jsonRepresentation()}");
var userid;
userid = deviceState?.userId;
print(userid);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: true,
child: WebView(
initialUrl: "https://app.pnlbase.com/login?playerid",
javascriptMode: JavascriptMode.unrestricted,
),
),
);
}
}
I want to disable the landscape mode. I tried to allow only portrait mode using following code. But it is not working with my physical device. How to solve this?
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(MyApp());
}
You need to paste the code in Widget build(). Consider this answer for more details
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}
When removing system overlays with SystemChrome.setEnabledSystemUIOverlays([]) the app does not expand to fill the screen.
Complete main.dart file:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Container(
color: Colors.red,
),
);
}
}
Without SystemChrome.setEnabledSystemUIOverlays([]):
Flutter version: 1.22.2
I'm unable to reproduce with Flutter 1.22(stable) but in https://github.com/flutter/flutter/issues/14432 people seem to have had good results with
setting Scaffold.resizeToAvoidBottomPadding to true.
You can also try to update Flutter and/or changing channels. Perhaps even trying on a physical device.
I was learning and trying to create one app using one stateful widget to display a list. My code looks like the following:
main.dart
import 'package:flutter/material.dart';
import './widgets/user_transactions.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: UserTransactions(),
),
),
);
}
}
and the stateul widget code is:
user_transactions.dart
import 'package:flutter/material.dart';
import '../models/transaction.dart';
class UserTransactions extends StatefulWidget {
#override
_UserTransactionsState createState() => _UserTransactionsState();
}
class _UserTransactionsState extends State<UserTransactions> {
final List<Transaction> _userTransactionLists = [
Transaction(name: 'boss 01'),
Transaction(name: 'boss 02'),
Transaction(name: 'boss 03'),
Transaction(name: 'boss 04'),
];
#override
Widget build(BuildContext context) {
print('==========================================');
print(_userTransactionLists.length);
return Column(
children: _userTransactionLists.map((tx) {
Text(tx.name);
print(tx.name);
}).toList(),
);
}
}
And the transaction class looks like this:
Transaction.dart
import 'package:flutter/foundation.dart';
class Transaction {
final String name;
Transaction({#required this.name});
}
But getting the error:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building UserTransactions(dirty, state:
_UserTransactionsState#c1fe3):
Column's children must not contain any null values, but a null value was
found at index 0
The relevant error-causing widget was:
UserTransactions org-dartlang-app:///packages/test_01/main.dart:14:18
I tried long time but still could not figure out. And when I was tying to debug, I am geeing the correct output using print line. It looks like the following:
#override
Widget build(BuildContext context) {
print('==========================================');
print(_userTransactionLists.length);
return Column(
children: _userTransactionLists.map((tx) {
print(tx.name);
return Text(tx.name);
}).toList(),
);
}
You are getting this error because you didn't return any value so, your tolist() method was retuning a list of null objects
Hot reload is not working in a simple Hello World example. When trying to change the text, a string, to something else under run-time and hot reload it, nothing happens. I'm debugging on a physical device and I'm using VSCode.
import 'package:flutter/material.dart';
void main() {
String text = "Hello world";
runApp(Center(child: new Text(text, textDirection: TextDirection.ltr)));
}
Is hot reload not reliable or am i doing something wrong here?
EDIT: Found out that restarting the app, CTRL+SHIFT+F5, worked as the hot reload should.
Specifically, a hot reload causes all the existing widgets to rebuild. Only code involved in the rebuilding of the widgets are automatically re-executed.
https://flutter.dev/docs/development/tools/hot-reload
This means that you need a widget class which implements the build method to ensure the code is going to be re-executed on a hot reload.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
String text = "Hello world";
return Center(
child: new Text(text, textDirection: TextDirection.ltr),
);
}
}
If you create your class like this i think there are no problems :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
String text="Hello World"
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(text,textDirection: TextDirection.ltr),
)));
}
}
A few types of code changes cannot be hot reloaded though:
Global variable initializers
Static field initializers
The main() method of the app
For these changes you can fully restart your application, without having to end your > debugging session:
Don’t click the Stop button; simply re-click the Run button (if in a run session) or Debug button (if in a debug session), or shift-click the ‘hot reload’ button.
Found this on the Flutter developer portal.