Skip to content

Instantly share code, notes, and snippets.

@yahya-uddin
Last active December 13, 2019 00:04
Show Gist options
  • Save yahya-uddin/cc7d47c4e919d5c6778473be58e87322 to your computer and use it in GitHub Desktop.
Save yahya-uddin/cc7d47c4e919d5c6778473be58e87322 to your computer and use it in GitHub Desktop.
Flutter Web Issue
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) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
(function() {
var _currentDirectory = (function () {
var _url;
var lines = new Error().stack.split('\n');
function lookupUrl() {
if (lines.length > 2) {
var match = lines[1].match(/^\s+at (.+):\d+:\d+$/);
// Chrome.
if (match) return match[1];
// Chrome nested eval case.
match = lines[1].match(/^\s+at eval [(](.+):\d+:\d+[)]$/);
if (match) return match[1];
// Edge.
match = lines[1].match(/^\s+at.+\((.+):\d+:\d+\)$/);
if (match) return match[1];
// Firefox.
match = lines[0].match(/[<][@](.+):\d+:\d+$/)
if (match) return match[1];
}
// Safari.
return lines[0].match(/(.+):\d+:\d+$/)[1];
}
_url = lookupUrl();
var lastSlash = _url.lastIndexOf('/');
if (lastSlash == -1) return _url;
var currentDirectory = _url.substring(0, lastSlash + 1);
return currentDirectory;
})();
var baseUrl = (function () {
// Attempt to detect --precompiled mode for tests, and set the base url
// appropriately, otherwise set it to '/'.
var pathParts = location.pathname.split("/");
if (pathParts[0] == "") {
pathParts.shift();
}
if (pathParts.length > 1 && pathParts[1] == "test") {
return "/" + pathParts.slice(0, 2).join("/") + "/";
}
// Attempt to detect base url using <base href> html tag
// base href should start and end with "/"
if (typeof document !== 'undefined') {
var el = document.getElementsByTagName('base');
if (el && el[0] && el[0].getAttribute("href") && el[0].getAttribute
("href").startsWith("/") && el[0].getAttribute("href").endsWith("/")){
return el[0].getAttribute("href");
}
}
// return default value
return "/";
}());
var mapperUri = baseUrl + "packages/build_web_compilers/src/" +
"dev_compiler_stack_trace/stack_trace_mapper.dart.js";
var requireUri = baseUrl +
"packages/build_web_compilers/src/dev_compiler/require.js";
var mainUri = _currentDirectory + "main_web_entrypoint.dart.bootstrap";
if (typeof document != 'undefined') {
var el = document.createElement("script");
el.defer = true;
el.async = false;
el.src = mapperUri;
document.head.appendChild(el);
el = document.createElement("script");
el.defer = true;
el.async = false;
el.src = requireUri;
el.setAttribute("data-main", mainUri);
document.head.appendChild(el);
} else {
importScripts(mapperUri, requireUri);
require.config({
baseUrl: baseUrl,
});
// TODO: update bootstrap code to take argument - dart-lang/build#1115
window = self;
require([mainUri + '.js']);
}
})();
/* ENTRYPOINT_EXTENTION_MARKER */
(function() {
var _currentDirectory = (function () {
var _url;
var lines = new Error().stack.split('\n');
function lookupUrl() {
if (lines.length > 2) {
var match = lines[1].match(/^\s+at (.+):\d+:\d+$/);
// Chrome.
if (match) return match[1];
// Chrome nested eval case.
match = lines[1].match(/^\s+at eval [(](.+):\d+:\d+[)]$/);
if (match) return match[1];
// Edge.
match = lines[1].match(/^\s+at.+\((.+):\d+:\d+\)$/);
if (match) return match[1];
// Firefox.
match = lines[0].match(/[<][@](.+):\d+:\d+$/)
if (match) return match[1];
}
// Safari.
return lines[0].match(/(.+):\d+:\d+$/)[1];
}
_url = lookupUrl();
var lastSlash = _url.lastIndexOf('/');
if (lastSlash == -1) return _url;
var currentDirectory = _url.substring(0, lastSlash + 1);
return currentDirectory;
})();
var baseUrl = (function () {
// Attempt to detect --precompiled mode for tests, and set the base url
// appropriately, otherwise set it to '/'.
var pathParts = location.pathname.split("/");
if (pathParts[0] == "") {
pathParts.shift();
}
if (pathParts.length > 1 && pathParts[1] == "test") {
return "/" + pathParts.slice(0, 2).join("/") + "/";
}
// Attempt to detect base url using <base href> html tag
// base href should start and end with "/"
if (typeof document !== 'undefined') {
var el = document.getElementsByTagName('base');
if (el && el[0] && el[0].getAttribute("href") && el[0].getAttribute
("href").startsWith("/") && el[0].getAttribute("href").endsWith("/")){
return el[0].getAttribute("href");
}
}
// return default value
return "/";
}());
let modulePaths = {
"dart_sdk": "packages/build_web_compilers/src/dev_compiler/dart_sdk",
"packages/collection/collection": "packages/collection/collection.ddc",
"packages/collection/src/algorithms": "packages/collection/src/algorithms.ddc",
"packages/collection/src/canonicalized_map": "packages/collection/src/canonicalized_map.ddc",
"packages/collection/src/comparators": "packages/collection/src/comparators.ddc",
"packages/collection/src/iterable_zip": "packages/collection/src/iterable_zip.ddc",
"packages/collection/src/priority_queue": "packages/collection/src/priority_queue.ddc",
"packages/collection/src/utils": "packages/collection/src/utils.ddc",
"packages/flutter/animation": "packages/flutter/animation.ddc",
"packages/flutter/cupertino": "packages/flutter/cupertino.ddc",
"packages/flutter/foundation": "packages/flutter/foundation.ddc",
"packages/flutter/gestures": "packages/flutter/gestures.ddc",
"packages/flutter/material": "packages/flutter/material.ddc",
"packages/flutter/painting": "packages/flutter/painting.ddc",
"packages/flutter/physics": "packages/flutter/physics.ddc",
"packages/flutter/rendering": "packages/flutter/rendering.ddc",
"packages/flutter/semantics": "packages/flutter/semantics.ddc",
"packages/flutter/src/animation/animation": "packages/flutter/src/animation/animation.ddc",
"packages/flutter/src/cupertino/action_sheet": "packages/flutter/src/cupertino/action_sheet.ddc",
"packages/flutter/src/foundation/_bitfield_web": "packages/flutter/src/foundation/_bitfield_web.ddc",
"packages/flutter/src/gestures/arena": "packages/flutter/src/gestures/arena.ddc",
"packages/flutter/src/painting/_network_image_web": "packages/flutter/src/painting/_network_image_web.ddc",
"packages/flutter/src/physics/clamped_simulation": "packages/flutter/src/physics/clamped_simulation.ddc",
"packages/flutter/src/rendering/animated_size": "packages/flutter/src/rendering/animated_size.ddc",
"packages/flutter/src/semantics/binding": "packages/flutter/src/semantics/binding.ddc",
"packages/flutter/src/widgets/actions": "packages/flutter/src/widgets/actions.ddc",
"packages/flutter/widgets": "packages/flutter/widgets.ddc",
"packages/flutter_web_2/main": "packages/flutter_web_2/main.ddc",
"packages/flutter_web_2/main_web_entrypoint": "packages/flutter_web_2/main_web_entrypoint.ddc",
"packages/meta/meta": "packages/meta/meta.ddc",
"packages/typed_data/typed_buffers": "packages/typed_data/typed_buffers.ddc",
"packages/vector_math/hash": "packages/vector_math/hash.ddc",
"packages/vector_math/vector_math_64": "packages/vector_math/vector_math_64.ddc"
};
if(!window.$dartLoader) {
window.$dartLoader = {
appDigests: _currentDirectory + 'main_web_entrypoint.digests',
moduleIdToUrl: new Map(),
urlToModuleId: new Map(),
rootDirectories: new Array(),
// Used in package:build_runner/src/server/build_updates_client/hot_reload_client.dart
moduleParentsGraph: new Map(),
moduleLoadingErrorCallbacks: new Map(),
forceLoadModule: function (moduleName, callback, onError) {
// dartdevc only strips the final extension when adding modules to source
// maps, so we need to do the same.
if (moduleName.endsWith('.ddc')) {
moduleName = moduleName.substring(0, moduleName.length - 4);
}
if (typeof onError != 'undefined') {
var errorCallbacks = $dartLoader.moduleLoadingErrorCallbacks;
if (!errorCallbacks.has(moduleName)) {
errorCallbacks.set(moduleName, new Set());
}
errorCallbacks.get(moduleName).add(onError);
}
requirejs.undef(moduleName);
requirejs([moduleName], function() {
if (typeof onError != 'undefined') {
errorCallbacks.get(moduleName).delete(onError);
}
if (typeof callback != 'undefined') {
callback();
}
});
},
getModuleLibraries: null, // set up by _initializeTools
};
}
let customModulePaths = {};
window.$dartLoader.rootDirectories.push(window.location.origin + baseUrl);
for (let moduleName of Object.getOwnPropertyNames(modulePaths)) {
let modulePath = modulePaths[moduleName];
if (modulePath != moduleName) {
customModulePaths[moduleName] = modulePath;
}
var src = window.location.origin + '/' + modulePath + '.js';
if (window.$dartLoader.moduleIdToUrl.has(moduleName)) {
continue;
}
$dartLoader.moduleIdToUrl.set(moduleName, src);
$dartLoader.urlToModuleId.set(src, moduleName);
}
// Whenever we fail to load a JS module, try to request the corresponding
// `.errors` file, and log it to the console.
(function() {
var oldOnError = requirejs.onError;
requirejs.onError = function(e) {
if (e.requireModules) {
if (e.message) {
// If error occurred on loading dependencies, we need to invalidate ancessor too.
var ancesor = e.message.match(/needed by: (.*)/);
if (ancesor) {
e.requireModules.push(ancesor[1]);
}
}
for (const module of e.requireModules) {
var errorCallbacks = $dartLoader.moduleLoadingErrorCallbacks.get(module);
if (errorCallbacks) {
for (const callback of errorCallbacks) callback(e);
errorCallbacks.clear();
}
}
}
if (e.originalError && e.originalError.srcElement) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
var message;
if (this.status == 200) {
message = this.responseText;
} else {
message = "Unknown error loading " + e.originalError.srcElement.src;
}
console.error(message);
var errorEvent = new CustomEvent(
'dartLoadException', { detail: message });
window.dispatchEvent(errorEvent);
}
};
xhr.open("GET", e.originalError.srcElement.src + ".errors", true);
xhr.send();
}
// Also handle errors the normal way.
if (oldOnError) oldOnError(e);
};
}());
var baseUrl = (function () {
// Attempt to detect --precompiled mode for tests, and set the base url
// appropriately, otherwise set it to '/'.
var pathParts = location.pathname.split("/");
if (pathParts[0] == "") {
pathParts.shift();
}
if (pathParts.length > 1 && pathParts[1] == "test") {
return "/" + pathParts.slice(0, 2).join("/") + "/";
}
// Attempt to detect base url using <base href> html tag
// base href should start and end with "/"
if (typeof document !== 'undefined') {
var el = document.getElementsByTagName('base');
if (el && el[0] && el[0].getAttribute("href") && el[0].getAttribute
("href").startsWith("/") && el[0].getAttribute("href").endsWith("/")){
return el[0].getAttribute("href");
}
}
// return default value
return "/";
}());
;
require.config({
baseUrl: baseUrl,
waitSeconds: 0,
paths: customModulePaths
});
const modulesGraph = new Map();
function getRegisteredModuleName(moduleMap) {
if ($dartLoader.moduleIdToUrl.has(moduleMap.name + '.ddc')) {
return moduleMap.name + '.ddc';
}
return moduleMap.name;
}
requirejs.onResourceLoad = function (context, map, depArray) {
const name = getRegisteredModuleName(map);
const depNameArray = depArray.map(getRegisteredModuleName);
if (modulesGraph.has(name)) {
// TODO Move this logic to better place
var previousDeps = modulesGraph.get(name);
var changed = previousDeps.length != depNameArray.length;
changed = changed || depNameArray.some(function(depName) {
return !previousDeps.includes(depName);
});
if (changed) {
console.warn("Dependencies graph change for module '" + name + "' detected. " +
"Dependencies was [" + previousDeps + "], now [" + depNameArray.map((depName) => depName) +"]. " +
"Page can't be hot-reloaded, firing full page reload.");
window.location.reload();
}
} else {
modulesGraph.set(name, []);
for (const depName of depNameArray) {
if (!$dartLoader.moduleParentsGraph.has(depName)) {
$dartLoader.moduleParentsGraph.set(depName, []);
}
$dartLoader.moduleParentsGraph.get(depName).push(name);
modulesGraph.get(name).push(depName);
}
}
};
define("main_web_entrypoint.dart.bootstrap", ["packages/flutter_web_2/main_web_entrypoint", "dart_sdk"], function(app, dart_sdk) {
dart_sdk.dart.setStartAsyncSynchronously(true);
dart_sdk._isolate_helper.startRootIsolate(() => {}, []);
var baseUrl = (function () {
// Attempt to detect --precompiled mode for tests, and set the base url
// appropriately, otherwise set it to '/'.
var pathParts = location.pathname.split("/");
if (pathParts[0] == "") {
pathParts.shift();
}
if (pathParts.length > 1 && pathParts[1] == "test") {
return "/" + pathParts.slice(0, 2).join("/") + "/";
}
// Attempt to detect base url using <base href> html tag
// base href should start and end with "/"
if (typeof document !== 'undefined') {
var el = document.getElementsByTagName('base');
if (el && el[0] && el[0].getAttribute("href") && el[0].getAttribute
("href").startsWith("/") && el[0].getAttribute("href").endsWith("/")){
return el[0].getAttribute("href");
}
}
// return default value
return "/";
}());
dart_sdk._debugger.registerDevtoolsFormatter();
$dartLoader.getModuleLibraries = dart_sdk.dart.getModuleLibraries;
if (window.$dartStackTraceUtility && !window.$dartStackTraceUtility.ready) {
window.$dartStackTraceUtility.ready = true;
let dart = dart_sdk.dart;
window.$dartStackTraceUtility.setSourceMapProvider(
function(url) {
url = url.replace(baseUrl, '/');
var module = window.$dartLoader.urlToModuleId.get(url);
if (!module) return null;
return dart.getSourceMap(module);
});
}
if (typeof document != 'undefined') {
window.postMessage({ type: "DDC_STATE_CHANGE", state: "start" }, "*");
}
// Injected by webdev for build results support.
window.$dartAppId = "AsveSxf7vMU09lPzqAEX+g==";
window.$dartRunMain = (app.lib__main_web_entrypoint || app.main_web_entrypoint).main;;
window.$dartReloadConfiguration = "ReloadConfiguration.none";
window.$dartLoader.forceLoadModule("dwds/src/injected/client");
window.$dartModuleStrategy = "require";
window.$dartUriBase = "http://localhost:22797";
window.$loadModuleConfig = require;
window.$dartExtensionUri = "http://localhost:22865/$debug";
var bootstrap = {
hot$onChildUpdate: function(childName, child) {
// Special handling for the multi-root scheme uris. We need to strip
// out the scheme and the top level directory, to match the source path
// that chrome sees.
if (childName.startsWith('org-dartlang-app:///')) {
childName = childName.substring('org-dartlang-app:///'.length);
var firstSlash = childName.indexOf('/');
if (firstSlash == -1) return false;
childName = childName.substring(firstSlash + 1);
}
if (childName === "package:flutter_web_2/main_web_entrypoint.dart") {
// Clear static caches.
dart_sdk.dart.hotRestart();
window.$dartRunMain = child.main;
return true;
}
}
}
dart_sdk.dart.trackLibraries("main_web_entrypoint.dart.bootstrap", {
"main_web_entrypoint.dart.bootstrap": bootstrap
}, '');
return {
bootstrap: bootstrap
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment